I've stumbled upon a strange situation where having reflect.runtime.universe._
imported causes reflect.runtime.universe.RuntimeClass
to be inferred where it seems Nothing
would be more appropriate.
Consider this simple method and List
:
import scala.reflect.ClassTag
def find[A : ClassTag](l: List[Any]): Option[A] =
l collectFirst { case a: A => a }
val list = List(1, "a", false)
I can use it to find the first element in the List
of some type, and it works well, as expected.
scala> find[String](list)
res1: Option[String] = Some(a)
scala> find[Long](list)
res2: Option[Long] = None
If I do not supply the type parameter, then A
is inferred as Nothing
, so I get Option[Nothing]
, also as expected.
scala> find(list)
res3: Option[Nothing] = None
But, if I import scala.reflect.runtime.universe._
and again do not supply the type parameter, A
is now inferred as reflect.runtime.universe.RuntimeClass
instead of Nothing
.
scala> find(list)
res4: Option[reflect.runtime.universe.RuntimeClass] = None
^ What?
This isn't a huge problem, since I can hardly imagine much of a use-case for the find
method without supplying the type parameter manually, but why does this happen? The ClassTag
seems partially to blame, as removing it once again causes Nothing
to be inferred (though completely breaks the method due to erasure). What's going on here?