Open devmage opened 9 years ago
Stumbled on this. It's very annoying.
It actually is very aware that it is Scala, and that Scala is not very good at type-inference. For some reason Scala infers the type j.u.List.
As usual in Scala, some judicious use of explicit type parameters solves the day. The first parameter is the fully specified container, and the second is the element type:
scala> val genBarList = for { ints <- genInts; bars <- Gen.sequence[List[Bar], Bar] { ints.map(genBarWith) } } yield bars genBarList: org.scalacheck.Gen[List[Bar]] = org.scalacheck.Gen$$anon$6@36ffdb7f
cheers, jed.
On 26 May 2015 at 07:32, Allan Douglas R. de Oliveira < notifications@github.com> wrote:
Stumbled on this. It's very annoying.
— Reply to this email directly or view it on GitHub https://github.com/rickynils/scalacheck/issues/131#issuecomment-105321385 .
@jedws in my example above, I offer exactly your proposed solution in genBarList2
, so thank you for confirming the behavior on your side. :)
The point here, however, is that without explicit type parameters the resultant List[A]
is a java.util.ArrayList[A]
instead of a scala.collection.immutable.List[A]
. It figures out the A
just fine; I'm accustomed to this being a problem, rather than the collection type.
One would think that a Scala library method would try to infer first to Scala types, e.g. here Scala collections. Instead, we see that Gen.sequence
seems to infer first to a Java collection. Small annoyance, but it exists.
@devmage sorry, didn't see your solution
Unfortunately, Scala's type inference is not very good. It is often necessary to explicitly annotate in places where it seems silly and that the compiler should do a better job of it.
Could scalacheck have a better signature, or provide a better default Buildable
? It probably can! That would be a great PR.
Consider the following console session, with ScalaCheck 1.12.1:
Incidentally, the type parameters on
genBarList2
cause IntelliJ to refuse to compile, although sbt and the REPL are fine with it.To get back to Scala-land, I have to import
scala.collection.JavaConverters._
and map on the generator with.map(_.asScala.toList)
.Happy New Year! :smiley: