This compiles and works as expected:
import scala.collection.JavaConverters._
val list1: java.util.List[Class[_]] = List[Class[_]](classOf[Any]).asJava
But I expected the following to give exactly the same result
val list2: java.util.List[Class[_]] = List(classOf[Any]: Class[_]).asJava
because Person.getClass
is typed with the same expected type, and it should be inferred as the type parameter for List.apply
.
Instead we get (in Scala 2.12.8)
type mismatch;
found : java.util.List[Class[_$2]] where type _$2
required: java.util.List[Class[_]]
Note: Class[_$2] <: Class[_], but Java-defined trait List is invariant in type E.
You may wish to investigate a wildcard type such as `_ <: Class[_]`. (SLS 3.2.10)
What am I missing? Or is this a bug?
The original question I was answering: https://stackoverflow.com/questions/55320574/how-to-call-java-method-taking-parameter-as-listclass-from-scala/55321244