object Test {
def main(args: Array[String]) {
val list: List[Double] = List(1.0, 2.0, 3.0, 4.0)
val none = None
case class Test()
val test = Test()
def f(x: Any) = x match {
case _: Some[Test] => println("_ matched")
case None => println("None matched")
}
f(list)
f(none)
f(test)
}
}
尝试编译上述代码会导致“被删除消除”编译时警告。
$>scalac Test.scala
Test.scala:11: warning: non-variable type argument Test in type pattern
Some[Test] is unchecked since it is eliminated by erasure
case _: Some[Test] => println("_ matched")
^
one warning found
我读了这篇备受推崇的 Stackoverflow post ,但我不明白这里的类型删除。
请您参考如下方法:
在运行时 jvm没有Some[Test]或 Some[String]只有Some[Any] .所以你不能匹配 Some[Test] .
在这种情况下,您可以匹配 Some 的内容像这样:
case Some(e: Test) => println(s"$e matched")




