我定义了一个自定义提取器来获取列表的最后一个元素,如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)
}
}
现在这匹配“好”:
List(1, 2, 3) match {
case init :+ last => "good"
case head :: tail => "bad"
}
但是如果我添加另一个子句,它现在突然匹配“坏”:
List(1, 2, 3) match {
case List(7) => "never"
case init :+ last => "good"
case head :: tail => "bad"
}
这种行为的原因是什么?