6

I'm trying to figure out what's going on on this piece of code, trying to figure out if there is something I don't understand or if it is a compiler bug or unintuitive spec, let's define these two almost identical functions:

def typeErause1(a: Any) = a match {
    case x: List[String] => "stringlists"
    case _ => "uh?"
}
def typeErause2(a: Any) = a match {
    case List(_, _) => "2lists"
    case x: List[String] => "stringlists"
    case _ => "uh?"
}

now if I call typeErause1(List(2,5,6)) I get "stringlists" because even if it is actually List[Int] with type erasure it is not able to tell the difference. But strangely if I call typeErause2(List(2,5,6)) I get "uh?" and I don't understand why it is not matching List[String] like it did before. If I use List[_] instead on the second function it is able to match it correctly which makes me think this is a bug in scalac.

I'm using Scala 2.9.1

Community
  • 1
  • 1
ilcavero
  • 3,012
  • 1
  • 31
  • 27
  • 2
    It's a bug in the matcher -- there are plenty tickets about it, and it should work starting with Scala 2.10.x. – Daniel C. Sobral Apr 08 '12 at 05:13
  • do you have a link to the ticket so I can choose you as the answer? – ilcavero Apr 08 '12 at 13:14
  • 1
    As I said, there are plenty tickets about the matcher, and I'm really not particularly interested in hunting for the one that covers this particular case. – Daniel C. Sobral Apr 08 '12 at 23:52
  • well thanks for your opinion but I was looking for a sustained reason and "it's a bug in the matcher because there are plenty of tickets about the matcher" is not one. – ilcavero Apr 09 '12 at 02:13
  • I'm not "opining" here, and you have as much access to the ticket list as I am. Stack Overflow is not "let others do the look up for me". – Daniel C. Sobral Apr 09 '12 at 12:21
  • @ilcavero - Daniel didn't answer; he left a comment. You're welcome to wait for someone to answer with supporting links, or look it up and answer yourself. Rudeness, however, [is not welcome](http://stackoverflow.com/faq#etiquette). – leedm777 Apr 09 '12 at 14:15

1 Answers1

1

It's a bug in the matcher ;) The pattern matcher is being (has been?) rewritten for 2.10

I just checked with the latest nightly and your code works as expected:

Welcome to Scala version 2.10.0-20120426-131046-b1aaf74775 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def typeErause1(a: Any) = a match {
     |     case x: List[String] => "stringlists"
     |     case _ => "uh?"
     | }
warning: there were 2 unchecked warnings; re-run with -unchecked for details
typeErause1: (a: Any)String

scala> def typeErause2(a: Any) = a match {
     |     case List(_, _) => "2lists"
     |     case x: List[String] => "stringlists"
     |     case _ => "uh?"
     | }
warning: there were 3 unchecked warnings; re-run with -unchecked for details
typeErause2: (a: Any)String

scala> typeErause1(List(2,5,6))
res0: String = stringlists

scala> typeErause2(List(2,5,6)) 
res1: String = stringlists
Community
  • 1
  • 1
rxg
  • 3,777
  • 22
  • 42