为什么isInstanceOf[T]
方法不能正常工作?
在下面,我定义了一个hello
类和伴随对象。在Hello对象中,我在代码行“hel.typetest[Int]
”中测试了this.isInstanceOf[T]
,当类型T
是Int
时,为什么这是true
?
object hello {
def main(args: Array[String]): Unit = {
Console.println("main")
val hel = new hello
hel.typetest[Int]
}
}
class hello {
def typetest[T: ClassTag]: Unit = {
Console.println(this.isInstanceOf[T])
Console.println(this.getClass)
}
}
产出:
main
true
class hello
2条答案
按热度按时间6gpjuf901#
因为type erasure(连同拳击)。
T
擦除为Object
,因此this.isInstanceOf[T]
在字节码中变为this.isInstanceOf[Object]
,这始终是正确的。碰巧,
ClassTag
旨在避免这种情况,但您需要实际使用它,而不是调用isInstanceOf
:当存在
ClassTag
时,还有用于模式匹配T
的特殊情况support:当
ClassTag
也存在时,有人建议让is
/asInstanceOf[T]
正常工作,但编译器中内置了一些假设,这些假设阻止了这一点,并且太难更改(如果我没有记错原因的话)。svmlkihl2#
在搜索了许多帖子并与类型擦除警告作斗争后,我在模式匹配中发现了这个堆栈溢出答案隐式ClassTag
基于那篇文章,我创建了下面的隐式类型匹配器来检查对象是否是泛型类型T的示例。希望这能对某些人有所帮助。
**编辑:经过进一步研究,您需要使用classTag[T].runtimeClass.isInstance(objectToCheck),而不是使用isInstanceOf[T],正如Alexey Romanov的回答所指出的那样。