有人能解释一下为什么这会给出擦除警告吗?
def optionStreamHead(x: Any) =
x match {
case head #:: _ => Some(head)
case _ => None
}提供:
warning: non variable type-argument A in type pattern scala.collection.immutable.Stream[A] is unchecked since it is eliminated by erasure
case head #:: _ => Some(head)我意识到我可以编写这个案例if (x.isInstanceOf[Stream[_]]) ...而不得到警告,但在我的案例中,我实际上希望使用模式匹配,并且有一大堆我不理解的警告看起来很糟糕
这里有一个同样令人费解的案例:
type IsStream = Stream[_]
("test": Any) match {
case _: Stream[_] => 1 // no warning
case _: IsStream => 2 // type erasure warning
case _ => 3
}发布于 2012-10-07 06:41:46
这两个都是2.9中的bug,在2.10中得到了解决。在2.10中,我们得到了一个新的模式匹配引擎(称为virtpatmat,用于虚拟模式匹配器):
scala> def optionStreamHead(x: Any) =
x match {
case head #:: _ => Some(head)
case _ => None
}
optionStreamHead: (x: Any)Option[Any]
scala> optionStreamHead(0 #:: Stream.empty)
res14: Option[Any] = Some(0)
scala> ("test": Any) match {
| case _: Stream[_] => 1 // no warning
| case _: IsStream => 2 // type erasure warning
| case _ => 3
| }
<console>:11: warning: unreachable code
case _: IsStream => 2 // type erasure warning
^
res0: Int = 3https://stackoverflow.com/questions/12764270
复制相似问题