If case class inheritance is prohibited, how to represent this?


Staff member
I am trying to create the case classes as explained in <a href="https://gleichmann.wordpress.com/20...ith-algebraic-datatypes-and-pattern-matching/" rel="noreferrer">this article</a>

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 (<a href="https://stackoverflow.com/questions/30454354/why-case-to-case-inheritance-is-prohibited-in-scala">Why case-to-case inheritance is prohibited in Scala</a>). 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