Exp
shouldn't use the case
keyword. That is, a sealed abstract case class
will rarely, if ever, make sense to use.
In this specific case, the only extra thing you get from sealed abstract case class Exp()
is an auto-generated companion object Exp
that has an unapply
method. And this unapply
method won't be very useful, because there isn't anything to extract from the generic Exp
. That is, you only care about decomposing Add
, Sub
, etc.
This is just fine:
sealed abstract class Exp
case class Literal(x: Int) extends Exp
case class Add(a: Exp, b: Exp) extends Exp
case class Sub(a: Exp, b: Exp) extends Exp
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…