我想知道,为什么 AnyVal 不能在 isInstanceOf 检查中使用? 这种行为背后的原因是什么?
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
在运行时不再存在。仅在编译时。换句话说,这只是一个编译器“技巧”,将 JVM 原语视为第一类对象。
但是,isInstanceOf
方法是在运行时执行的,因此无法工作。因此会出现编译器错误。
关于scala - 为什么 AnyVal 不能用于 isInstanceOf 检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11070859/