Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can AnyVal not be used in an isInstanceOf check?

Tags:

scala

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]
like image 392
John Threepwood Avatar asked Jun 17 '12 11:06

John Threepwood


1 Answers

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.

like image 196
paradigmatic Avatar answered Oct 17 '22 04:10

paradigmatic