I was wondering, why AnyVal can not be used in an isInstanceOf check ?
What is the reason behind this behavior ?
scala> val c = 't'
c: Char = t
scala> c.isInstanceOf[AnyVal]
<console>:12: error: type AnyVal cannot be used in a type pattern or isInstanceO
f test
c.isInstanceOf[AnyVal]
AnyValdoes not exist anymore at runtime. Only at compile-time. In other words, it’s just a compiler “trick” to consider the JVM primitives as first-class objects.However, the
isInstanceOfmethod is executed at runtime, so it cannot work. Hence the compiler error.