I use case classes for models in ORM. Each model has an identifier, but the identifier should not be publicly available. So I have a parent trait
trait WithId {
private var id: Long = 0
}
and many classes of cases (models) inheriting from it
case class C1(a: Int, b: String) extends WithId
case class C2(...) extends WithId
...
Now, if someone calls copy () in the case class, he does not copy the identifier with him, but sets it to 0.
val c1 = C1(3, "bla")
val c2 = c1.copy(b="bla2")
I want it to also copy the id.
Since I have many such classes, I would prefer to have as little code as possible in class classes. Thus, the implementation of the copy () method in each case will have a lot of template code.
- , copy() ? , - ? , ?
id ,
case class C1(a: Int, b: String, protected var id: Long)
. , case, , id case, case. , .