After reading this excellent question/answer on type erasure
in Scala, I tried this code. The Scala compiler did not output a type erasure
warning.
scala> val x: List[Int] = List(1,2,3)
x: List[Int] = List(1, 2, 3)
scala> x match {
| case List(x: Int) => println("a")
| case _ => println("false")
| }
false
Why doesn't the above code output the same warning as this code:
scala> List(1,2,3) match {
| case l: List[String] => println("list of strings")
| case _ => println("ok")
| }
<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 l: List[String] => println("list of strings")
^
list of strings
第一种情况不仅仅是测试类型 - 它通过模式匹配进行测试,列表只有一个整数元素。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.