Why can't AnyVal be used in checking isInstanceOf?

I was wondering why AnyVal cannot be used in checking isInstanceOf? What is the reason for 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]
+5
source share
1 answer

AnyValno longer exists at runtime. Only at compile time. In other words, it is simply a "compiler" trick to treat JVM primitives as first-class objects.

However, the method isInstanceOfis executed at run time, so it cannot work. Therefore, a compiler error.

+11
source

All Articles