Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly did Scala improve with pattern matching in 2.10?

I found it interesting that this puzzler, specifically this code:

val (i, j): (Int, Int) = ("3", "4")

Fails at runtime in Scala 2.9.1, but fails at compile time w/ 2.10 M3(which is great). I try to track what's coming in new Scala releases, but I'm unable to connect the dots here. What improvement led to this more precise behavior?

like image 567
Adam Rabung Avatar asked Sep 09 '25 19:09

Adam Rabung


2 Answers

In scala 2.10, the pattern matcher has had a complete re-write and is now the virtualized pattern matcher. Read more about it!

like image 137
oxbow_lakes Avatar answered Sep 14 '25 02:09

oxbow_lakes


The thing that's going on is that the new pattern matcher is much easier to enhance and maintain, because it's not a rats nest piece of code. The following example code should also exhibit the same change:

("3", "4") match { case (i, j): (Int, Int) => /* whatever */ }

What's happening is Scala understanding at compile time that the pattern can never be matched.

like image 44
Daniel C. Sobral Avatar answered Sep 14 '25 02:09

Daniel C. Sobral