I defined a custom extractor to get the last element of the list, as in https://stackoverflow.com/a/6697749/1092910:
object :+ {
def unapply[A](l: List[A]): Option[(List[A], A)] = {
if (l.isEmpty)
None
else
Some(l.init, l.last)
}
}
Now this matches "good":
List(1, 2, 3) match {
case init :+ last => "good"
case head :: tail => "bad"
}
But if I add another clause, it suddenly matches "bad" now:
List(1, 2, 3) match {
case List(7) => "never"
case init :+ last => "good"
case head :: tail => "bad"
}
What is the reason for this behaviour?
It's #1697/2337 and a dozen duplicates.
https://issues.scala-lang.org/browse/SI-1697
It looks safe to say that it will not be fixed in direct fashion, but by deleting the pattern matcher for the virtpatmat implementation. Try a recent build and compile with -Yvirtpatmat, you'll get the right answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With