https://kotlinlang.org logo
#announcements
Title
# announcements
n

Nir

11/29/2019, 6:36 PM
In Kotlin, suppose I have two dataclasses that have some shared fields. In python dataclasses, I would solve this by factoring the shared fields into another dataclass, and then inheriting from both, and things like equality would work as expected. kotlin doesn't allow inheritance of dataclasses, so how would you solve this problem?
y

yawkat

11/30/2019, 2:30 PM
Use composition - either have one data class reference the other, or create a third data class that is used by both.
n

Nir

12/03/2019, 8:07 PM
@yawkat there's extensive discussion on why that doesn't work. Basically, that causes nesting, whereas I want things ot be flat.
y

yawkat

12/03/2019, 9:45 PM
well, that's the idiomatic way to do it - what may be a good idea in other languages isn't necessarily a good idea in kotlin/java
2 Views