-6

Why does the following piece of code not work as expected? By looking at the code, I was thinking that it might return "a list of numbers" as numbers is a list with numbers. but I ran the code and got the output as "list of strings". Guess I cleared most of the confusion around here.

val numbers: List[Int] = List(1,2,3)
val numbersMatch: String = numbers match {
  case listOfStrings: List[String] => "a list of strings"
  case listOfNumbers: List[Int] => "a list of numbers"
  case _ => ""
}
println(numbersMatch)
Aki
  • 1,644
  • 12
  • 24
neil
  • 149
  • 1
  • 12
  • 3
    What happened when you ran it on a REPL? Did the outcome match the expectations? If not, why/how not? SO should not be used for a “human compiler”. – user2864740 Jan 08 '19 at 21:52
  • I didn't understand the concept of type erasure – neil Jan 08 '19 at 22:10
  • 1
    I think you meant that to be a comment on the answer posted. But can you clarify the question? You are asking for an answer, but don't have a question. I think you wanted to know what happens when you run the code, but the answer to that would be to just run the code yourself. If the output doesn't make sense, explain what your intuition is and where it fails, and then someone might go more in depth about Type Erasure or whatever your issue is. As is, you haven't asked a question, just posted a code snippet with no context. – Ethan Jan 08 '19 at 22:19
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – y2k-shubham Jan 08 '19 at 23:41
  • Your question is very unclear. For me, this code works exactly as expected. So, obviously, you seem to expect it to do something different than I do. But, you don't say what your expectations are. How can we tell you why the code doesn't do what you expect it to do when you don't tell us what you expect it to do? – Jörg W Mittag Jan 09 '19 at 07:02
  • I'm not sure why everybody's beating up on this guy. It's pretty obvious from his code what his expectation is. – Lasf Jan 09 '19 at 14:42

1 Answers1

7

This code will print that it is a list of string which is a result of the way generics are compiled on most JVM languages.

They use so called "Type erasure" which drops generics. Since the generic type is not available at runtime, Scala cannot differentiate between List[String] and List[Int] and therefore the first case matches. If you compile your code, Scala will actually warn you about this:

warning: fruitless type test: a value of type List[Int] cannot also be a List[String] (the underlying of List[String]) (but still might match its erasure)

case listOfStrings: List[String] => "a list of strings"

Community
  • 1
  • 1
Aki
  • 1,644
  • 12
  • 24