Derived Scala case class with same member variables as base

前端 未结 1 526
既然无缘
既然无缘 2021-02-06 01:27

Is there a nicer way of doing this?

scala> case class A(x : Int)
defined class A

scala> case class B(override val x : Int, y : Int) extends A(x)
defined c         


        
相关标签:
1条回答
  • 2021-02-06 01:54

    I would strongly advise not to inherit from a case class. It has surprising effects on equals and hashCode, and has been deprecated in Scala 2.8.

    Instead, define x in a trait or an abstract class.

    scala> trait A { val x: Int }
    defined trait A
    
    scala> case class B(val x: Int, y: Int) extends A
    defined class B
    

    http://www.scala-lang.org/node/3289

    http://www.scala-lang.org/node/1582

    0 讨论(0)
提交回复
热议问题