3

これが消去の警告を出す理由を誰かが説明できますか?

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
}
4

1 に答える 1

3

どちらも 2.10 で解決された 2.9 のバグです。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 = 3
于 2012-10-06T22:41:45.863 に答える