When using .isInstanceOf[GenericType[SomeOtherType]]
, where GenericType
and SomeOtherType
are arbitrary types (of suitable kind), the Scala compiler gives an unchecked warning due to type erasure:
scala> Some(123).isInstanceOf[Option[Int]]
<console>:8: warning: non variable type-argument Int in type Option[Int] is unchecked since it is eliminated by erasure
Some(123).isInstanceOf[Option[Int]]
^
res0: Boolean = true
scala> Some(123).isInstanceOf[Option[String]]
<console>:8: warning: non variable type-argument String in type Option[String] is unchecked since it is eliminated by erasure
Some(123).isInstanceOf[Option[String]]
^
res1: Boolean = true
However, if SomeOtherType
is itself a generic type (e.g. List[String]
), no warning is emitted:
scala> Some(123).isInstanceOf[Option[List[String]]]
res2: Boolean = true
scala> Some(123).isInstanceOf[Option[Option[Int]]]
res3: Boolean = true
scala> Some(123).isInstanceOf[Option[List[Int => String]]]
res4: Boolean = true
scala> Some(123).isInstanceOf[Option[(String, Double)]]
res5: Boolean = true
scala> Some(123).isInstanceOf[Option[String => Double]]
res6: Boolean = true
(recall that tuples and =>
are syntactic sugar for Tuple2[]
and Function2[]
generic types)
Why is no warning emitted? (All these are in the Scala REPL 2.9.1, with the -unchecked
option.)