-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow base classes for data classes #31
Comments
👍 👍 👍 |
The feature has been released in Kotlin 1.1, thus I'm closing the KEEP. Do not hesitate to open a YouTrack issue for any additional suggestions. |
Probably this has been said somewhere, but why exactly can't a data class inherit from another data class? and
I'd expect it to be generated that for Inherited class 'a' is component1 and 'b' is component2 and all the additional methods to be generated with this in mind. It would be super useful to create hierarchies like Resource and Book or Info and DetailedInfo or multitude of other use cases where I need to add variables to a class without repeating the ones from the base class. |
@grodzickir There is no way to implement hashCode and equals correctly for such hierarchy. It's not something Kotlin only, check for example AutoValue, they have exactly the same approach, you can extend abstract classes and interfaces, but cannot extend other value classes. |
OK, I've read a bit about this and I've found that there is no way of doing it only if we want to comply to Liskov Substitution Principle, that is "you can use a subclass everywhere where you use a superclass". I may be missing something here, but I think that generating proper equals() and hashCode() methods is perfectly possible, as we don't need to comply to LSP with them (heck, we can't even make a subclass right now). The equals() and hashCode() functions are meant to be used especially in HashMaps and other structures where an instance of Base class should never be equal to an instance of Inherited class. That said, even IntelliJ can generate a proper equals() function using getClass() method for comparison instead of 'instanceof' comparison which breaks the symmetry (according to mentioned Effective Java chapter). It even gives an option to accept subclasses but that makes equals() method not compliant. So not accepting the subclasses is the right behaviour in generated functions. A subclass instance never should equal to the superclass instance. |
@grodzickir: can you provide an example for equals and hashcode? I think you are right but want to make sure we understand the same thing. |
@stangls I'll post the example in Java as I'm more sure about it's correctness.
Checking for classes equality instead of 'instanceof' makes sure we are getting truly transitive and symmetrical methods. (I didn't include the hashCode implementation for brevity as it's straightforward) |
@grodzickir Even if you don't care about LSP, there is another important thing. And if you need a Base class, do you really want to make it |
Proposal
The text was updated successfully, but these errors were encountered: