If case class inheritance is forbidden, how is this represented?

I am trying to create case classes as described 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 get the following error in IntelliJ. I understand why this is forbidden ( Why accidental inheritance is forbidden in Scala ). What is the alternative 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
           ^
+4
source share
1 answer

ExpDo not use a keyword case. That is, sealed abstract case classrarely, if ever, it makes sense to use.

, sealed abstract case class Exp(), - - Exp, unapply. unapply , Exp. , Add, Sub ..

:

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
+9

Source: https://habr.com/ru/post/1627710/


All Articles