我在scala中有这种模式匹配
(col, el, rbt1, rbt2) match {
case (Black, z, Node(Red, y, Node(Red, x, a, b), c), d) => Node(Red, y, Node(Black, x, a, b), Node(Black, z, c, d))
case (Black, z, Node(Red, x, a, Node(Red, y, b, c)), d) => Node(Red, y, Node(Black, x, a, b), Node(Black, z, c, d))
case (Black, x, a, Node(Red, y, b, Node(Red, z, c, d))) => Node(Red, y, Node(Black, x, a, b), Node(Black, z, c, d))
case (Black, x, a, Node(Red, z, Node(Red, y, b, c), d)) => Node(Red, y, Node(Black, x, a, b), Node(Black, z, c, d))
case _ => Node(col, el, rbt1, rbt2)
}
所有情况在=>之后都做相同的事情(最后一个除外)。 因此,我尝试以这种方式更改代码:
(col, el, rbt1, rbt2) match {
case (Black, z, Node(Red, y, Node(Red, x, a, b), c), d)
| (Black, z, Node(Red, x, a, Node(Red, y, b, c)), d)
| (Black, x, a, Node(Red, y, b, Node(Red, z, c, d)))
| (Black, x, a, Node(Red, z, Node(Red, y, b, c), d)) => Node(Red, y, Node(Black, x, a, b), Node(Black, z, c, d))
case _ => Node(col, el, rbt1, rbt2)
}
但是它不能编译。 Scala说它不识别y,x等...为什么?如何在Scala中使用or-pattern?