What is the most idiomatic way of expressing a class hierarchy in Kotlin? I found this blog post which used delegation, but I have heard that delegation is not a feature that many developers choose to use. Though in this case I can’t see the downside to it. My specific use case is a set of immutable classes where sub classes add additional properties only. Thanks!
d
Dico
03/26/2020, 9:54 AM
The most idiomatic way to express a hierarchy of classes is by using plain inheritance.
Dico
03/26/2020, 9:55 AM
You may or may not want to partially flatten your hierarchy using composition.
Dico
03/26/2020, 9:56 AM
Delegation is interesting but not necessary to express composition, you can do this with class properties.
v
Victor Cardona
03/26/2020, 7:49 PM
True that you don’t need to use delegation to implement composition, but delegation does allow you to forgo chaining calls to properties:
A.prop
instead of
A.B.prop
. Well at least without having to write custom accessors.