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

后端 未结 1 1346
太阳男子
太阳男子 2020-12-20 12:34

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:E         


        
相关标签:
1条回答
  • 2020-12-20 13:16

    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
    
    0 讨论(0)
提交回复
热议问题