A data class cannot extend another data class but it can extend a normal open class. I was expecting this wouldn't work. 🤨
Why not allow data class to extend another data class?
What am I missing?
z
Zach Klippenstein (he/him) [MOD]
12/02/2021, 6:59 PM
The copy function on the parent data class, for one, wouldn’t know how to copy the subclass.
e
ephemient
12/02/2021, 7:00 PM
that might be solvable with overriding, but allowing data class inheritance will cause the equals method to be non-reflexive. I don't think there's a great way to handle that in general
👍 1
z
Zach Klippenstein (he/him) [MOD]
12/02/2021, 7:11 PM
there’s a lot of things about data classes that could be done in ways that would make them much more flexible with other features of the language. I think another reason is that you probably couldn’t rely on as many of the guarantees of a data class if it could have subclasses, since subclasses could break those contracts
It could be solved by forcing to override the copy method and the equals method in the subclass... easy I think. 🙂 Anyway I'll keep the open class for now.