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]
AnyVal
does 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 isInstanceOf
method is executed at runtime, so it cannot work. Hence the compiler error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With