I’ve been reading on composition over inheritance ...
# announcements
p
I’ve been reading on composition over inheritance in Kotlin and came across a few articles like this https://medium.com/the-kotlin-chronicle/why-you-should-use-class-delegation-in-kotlin-fb0a3ebf151e One way Kotlin helps to reduce boiler plate code is by using class delegation and the
by
keyword. This looks great and I like it so far. However, during the closing panel of KotlinConf 2018, Andrey and another guy said it was their least favourite feature. Does anyone have any opinion or bad experience with class delegation?
☝️ 4
@ghedeon @sindrenm Are you both saying you had bad experiences with class delegation? 😅
s
Nah, I love it!
Or, I love that it exists. But always use the right tool for the job. Inheritance has its place, composition has its place and inheritance by delegation has its place. I would just consider the use case and choose whichever tool you feel works best. Which is the worlds most boring answer, but there it is. 😛
👍 1
g
My reaction was more like: "Also interested in this subject". You put it nicely: after those words at kotlinkonf it's not clear what's the deal with class delegation.
😁 1
s
Honestly, Andrey's “I really hate inheritance by delegation” doesn't really give me much to go by. Would love to hear a more thorough explanation. simple smile
👍 1
p
I believe that they don’t like how this feature is implemented (supported) in a language, and not that the feature in itself is bad. The syntax is a bit limited right now and it would be great to have more general solution. For example, I have found this KEEP https://github.com/Dico200/KEEP/blob/ibd-enhancements/proposals/implementation-by-delegation-enhancements.md
💯 1
p
Thanks guys! Also @Pavlo Liapota you may actually be right there. Perhaps it’s to do with the way class delegation is implemented as you say. It would be nice to see examples where the behaviour is unexpected or where the current implementation has limitations
k
It's frustratingly limited right now, eg. you can't get access to the delegate instance at all.
👍 1
s
That would make sense. It does indeed have its limits, but the idea is good. Thanks!
p
That’s a very good point actually!