Subclassing a data class, copy method

Is there a way to do this {code} data open class A(val a:String) data class B(a:String, val b:String):A(a) {code}

When I try this, I get Function ‘component1’ generated for the data class conflicts with members of supertype A

I would like to be able to use the copy method for class B

I've run into this too. I don't think there is a workaround at present. The open-ness doesn't propagate to the generated methods.

In Scala it is not allowed to subclass case clases for a good reason.

This is not enforced by compiler (yet?), but basically data class should be final. There was a recent discussion internally if we want to support abstract data classes, so that data class could be either final or abstract. It allows for some usecases with sharing common data across a number of data classes, but we didn't decide or even investigate it thoroughly.

Hi there,

Any chance of splitting copy method generation out from data classes? E.g. using a ‘copy’ annotation. I almost always want a copy method but often not equality or to string.

If I want to customise equality for example I have to ditch data classes completely and painfully implement copy manually.


1) Yes, we are planning to do so 2) You can customize equality in your data classes

Awesome on both counts!

I swear I tried overriding equals fairly recently and it complained. Anyway, I’m happy to be wrong!


For 1) Will there be a trait that says that some type has a copy method like {code} trait HasAB : Copy<T> {   var a:Int   var b:String } {code}

I have been playng with RxJava a lot and I often need to make copy of immutable data