Limitations
For now, you cannot inherit another class when defining a data class. To avoid delaying the 1.0 release, the makers of Kotlin have decided to have this restriction to avoid the problems that would be caused by this. Imagine a data class, Derived, inherits from a data class, Base; if this happens, then these questions need to be answered:
- Should an instance of Base be equal to an instance of Derived if they have the same values for all the shared fields?
- What if I copy an instance of Derived through a reference of the type Base?
I am sure, in the future, all the limitations will be addressed and we would be able to write code similar to this (the Scala developer would be familiar with the construct of Either
):
sealed abstract class Either<out L, out R> { data class Left<out L, out R>(val value: L) : Either<L, R>() data class Right<out L, out R>(val value: R) : Either<L, R>() }