有人可以告诉我如何避免下面的代码块中的警告:
abstract class Foo[T <: Bar]{
case class CaseClass[T <: Bar](t: T)
def method1 = {
case CaseClass(t: T) => println(t)
csse _ =>
}
}
这会导致编译器警告:
abstract type pattern T is unchecked since it is eliminated by erasure
case CaseClass(t: T) => println(t)
^
2 回答
你可以使用
ClassTag
(或TypeTag
):另一种使用的变体,特别是如果你想使用
trait
(而不是使用其他解决方案需要的class
或abstract class
),看起来像这样: