我有一个案例类,如下所示:
// parent class
sealed abstract class Exp()
// the case classes I want to match have compatible constructors
case class A (a : Exp, b : Exp) extends Exp
case class B (a : Exp, b : Exp) extends Exp
case class C (a : Exp, b : Exp) extends Exp
// there are other case classes extending Exp that have incompatible constructor, e.g.
// case class D (a : Exp) extends Exp
// case class E () extends Exp
// I don't want to match them
我想匹配:
var n : Exp = ...
n match {
...
case e @ A (a, b) =>
foo(e, a)
foo(e, b)
case e @ B (a, b) =>
foo(e, a)
foo(e, b)
case e @ C (a, b) =>
foo(e, a)
foo(e, b)
...
}
def foo(e : Exp, abc : Exp) { ... }
有没有办法将这三个案例合并为一个案例(不向 A、B、C 添加中间父类)?我无法更改 A、B、C 或 Exp 的定义。某一些:
var n : Exp = ...
n match {
...
case e @ (A | B | C) (a, b) => // invalid syntax
foo(e, a)
foo(e, b)
...
}
这显然不起作用,也没有:
var n : Exp = ...
n match {
...
case e @ (A (a, b) | B (a, b) | C (a, b)) => // type error
foo(e, a)
foo(e, b)
...
}