Why no Type Erasure Warning

Kevin Meredith

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
Ed Staub

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scala Type Erasure warning in Play Framework Application

From Dev

Scala Type Erasure warning in Play Framework Application

From Dev

Why not remove type erasure from the next JVM?

From Dev

Why not remove type erasure from the next JVM?

From Dev

Scala warning: abstract type pattern Z is unchecked since it is eliminated by erasure

From Dev

Why didn't Java Type Erasure prevents this code from compiling

From Dev

Which of the overloaded methods will be called on runtime if we apply type erasure, and why?

From Dev

Java type erasure: why won't the following snippet compile?

From Dev

Removing "eliminated by erasure" warning in Scala

From Dev

Why is there an incompatible pointer type warning?

From Dev

Why is there no underflow warning for unsigned type?

From Dev

Erasure of type fields

From Dev

Type Erasure in Scala

From Dev

Type erasure with parameter defaults

From Dev

Java type inference with erasure

From Dev

Scala isInstanceOf and type erasure

From Dev

Generics and type erasure

From Dev

Type erasure in Haskell?

From Dev

Generics type erasure in Java

From Dev

Type erasure with collections

From Dev

scala getClass and type erasure?

From Dev

Generics and type erasure

From Dev

Generics not performing Type Erasure

From Dev

Overcome type erasure in Java

From Dev

Why is the type of "Banana with Cream" the same as "Banana" after erasure ? How can it be fixed?

From Dev

Why is Java (but not .NET) unable to accommodate generics without the need for raw types / type erasure?

From Dev

type erasure in implementation of ArrayList in Java

From Dev

Swift type erasure / reification behavior?

From Dev

Is my understanding of 'type erasure' correct?

Related Related

HotTag

Archive