Suppose I have the following:
sealed trait T { val foo: Int }
case class A(foo: Int) extends T
case class B(foo: Int, bar: String) extends T
If I have a function like
def f(t: T): T = {
val foo = ...
t.copy(foo = foo)
}
I get an error: value copy is not a member of T. This is true; only A and B have a copy method. But since T is a sealed trait and the only classes extending T are case classes with synthetically generated copy methods, shouldn't the compiler be able to prove that all instances of T have a copy method?
I can get around this by doing the proof manually:
def f(t: T): T = {
val foo = ...
t match {
case a: A => a.copy(foo = foo)
case b: B => b.copy(foo = foo)
}
}
but this is repetitive and I'd rather the compiler be able to do this for me. From a high level: if a sealed trait with abstract vals is extended only by case classes, then generate a copy method for the trait only for the abstract vals it defines. Someone with more experience with Scala's internals could probably flesh this out a bit more.
Are there any issues with something like this? If a feature like this is desirable, I'd like to contribute a patch.