List(1,2) match {
case List(1,_) => println("1 in postion 1")
case _ => println("default")
}
编译/运行正常。同样如此。
List(1) match ...
List(3,4,5) match ...
但不包括
List() match ...
这将导致以下错误。
found : Int(1)
required : Nothing
case List(1,_) => println("1 in postion 1")
为什么List()会尝试匹配List(1,_)?
List[Int]
可强制转换为List[Nothing]
,但反之不行。 - missingfaktor