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?