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.)
I had a look to the Scala compiler sources, and I discovered something interesting looking at
which is where you find the warning. If you look carefully at line 1399 to:
which is where the the the warning are generated, you see some nested methods including the check method:
While I am not expert in the Scala compiler, we should all thanks the guys to making the code so self-explanatory. Let’s look inside the
tp matchblock and the treated cases:If you look to all other cases, there is a line which is also commented:
That tells you exactly what happen if your type has other type parameter (as Function2, or Tuple2). The check function returns unit without performing any test.
I do not for which reason this has been done this way, but you might want to open a bug at
https://issues.scala-lang.org/browse/SI
providing the code you posted here as an excellent test case, and reference to the Infer.scala source which I copied above.