为什么我看到“类型模式中的抽象类型T ...未经检查,因为它被擦除”,即使我有类型和类标签

时间:2017-01-06 02:46:33

标签: scala type-erasure

我有从Map中提取值的代码,我将其别名为:

type CsvRow = Map[String, Any]

和提取类:

class CsvExtractor(row: CsvRow) extends Extractor {
  def get[T: TypeTag: ClassTag](k: String): T = getOption[T](k).get

  def getOption[T: TypeTag: ClassTag](k: String): Option[T] = {
    row(k) match {
      case v: Some[T] => v
      case None       => None
    }
  }
}

编译器给了我这个警告:

[warn] /Users/axue/workspace/events/schema-kontrol/src/main/scala/com/lumoslabs/schemakontrol/core/extractor/CsvExtractor.scala:12: abstract type T in type pattern Some[T] is unchecked since it is eliminated by erasure
[warn]       case v: Some[T] => v
[warn]               ^

但ClassTag和TypeTag不应该确保存在该类型信息吗?

1 个答案:

答案 0 :(得分:0)

这是因为你将它放入泛化类定义中。尝试更改为以下内容:

case Some(v: T) => v