I am trying to create the case classes as explained in this article
sealed abstract case 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
However, I am getting the following error in IntelliJ. I understand why it is prohibited (Why case-to-case inheritance is prohibited in Scala). What is the alternate way here?
Error:(2, 13) case class Literal has case ancestor A$A34.A$A34.Exp, but case-to-case inheritance is prohibited. To overcome this limitation, use extractors to pattern match on non-leaf nodes.
case class Literal(x:Int) extends Exp
^
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