How to fix Product Type Inferred Error from Scala WartRemover Tool

I use the WartRemover tool to avoid possible errors in my Scala 2.11 code.

In particular, I want to know how to fix the "Product Type" error.

Looking at the repo documentation, I can only see an example of a crash, but I would like to know how I can fix this error:

https://github.com/puffnfresh/wartremover#product .

While doing my homework, I get this link that explains how to fix input errors like https://blog.cppcabrera.com/posts/scala-wart-remover.html . And I quote: β€œIf you see any of the warnings below, a fix is ​​usually as simple as providing type annotations,” but I don't understand what that means. I really need a concrete example.

+7
scala type-annotation
source share
2 answers

Product is a very abstract high level type with very few restrictions. When the output type is Product , this usually indicates that you made a mistake. For example. if you have:

 List((1, "hi", 0.4f), (2, "bye"), (3, "aloha", 7.2f)) 

This then compiles in order, giving you a List[Product] . But, as and when Any output, this is probably a mistake - you probably meant that it was List[(Int, String, Float)] and meant that it had a third record in the middle tuple.

If you really want List[Product] , you can avoid the warning about this by explicitly specifying an argument of the type:

 List[Product]((1, "hi", 0.4f), (2, "bye"), (3, "aloha", 7.2f)) 
+4
source share

Type annotations are nothing more than an explicit type indication, rather than leaving it to the type inference system.

The simplest example in this case might be:

 val element = 2 

The currently inferred type is Int . If you want to have more control over the type, for example, specify Byte, Short, Long, Double , you can explicitly specify the type as:

 val element: Double = 2 

Type annotations are also required for public methods like

Type inference can break encapsulation in these cases, because it depends on the internal method and class data

( Source )

+1
source share

All Articles