1

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
Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

3

The first case is not just testing type - it's testing by pattern-match that the list has exactly one integer element.

Ed Staub
  • 15,480
  • 3
  • 61
  • 91