在Scala上生活的一个可悲的事实是,如果实例化一个List [Int],则可以验证您的实例是一个List,并且可以验证它的任何单个元素是一个Int,而不是它是一个List [可以很容易地验证:
scala> List(1,2,3) match {
| case l : List[String] => println("A list of strings?!")
| case _ => println("Ok")
| }
warning: there were unchecked warnings; re-run with -unchecked for details
A list of strings?!
-unchecked选项将责任完全归咎于类型擦除:
scala> List(1,2,3) match {
| case l : List[String] => println("A list of strings?!")
| case _ => println("Ok")
| }
<console>:6: warning: non variable type-argument String in type pattern is unchecked since it is eliminated by erasure
case l : List[String] => println("A list of strings?!")
^
A list of strings?!
为什么会这样,我该如何解决?
scala 2.10.2
,我改为看到此警告:<console>:9: warning: fruitless type test: a value of type List[Int] cannot also be a List[String] (but still might match its erasure) case list: List[String] => println("a list of strings?") ^
我发现您的问题和答案非常有帮助,但是我不确定此更新的警告是否对读者有用。