Suppose I have some abstract value field defined in a trait:
trait Base {
val toBeOverride: String
}
case class Impl(other:Int) extends Base {
override val toBeOverride = "some value"
}
How can I write a function that I can easily get a cloned instance only overriding the toBeOverride
value, like this:
// copy only available to case class instance
// v does not have method 'copy'
def overrideBaseValue[T <: Base](v: Base) =
v.copy(toBeOverride = "prefix" + v.toBeOverride)
?
Edit
@som-snytt, I don't think this is a duplicate, just like a Trait
is not the same as an Abstract Class
. And the answers of that question do not satisfy me, see below.
@Blaisorblade, yes, it is a problem. For instances of each sub case class, the toBeOverride
field are the same, so it should not appear in the constructor.
For now all the suggestions are to define an customized copy
method in each(!) sub case class and that in my opinion is ugly and shows the incapability of the language.