https://kotlinlang.org logo
Title
p

Pascal How

03/29/2019, 11:10 AM
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

sindrenm

03/29/2019, 11:41 AM
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

ghedeon

03/29/2019, 11:44 AM
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

sindrenm

03/29/2019, 11:47 AM
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. 😒imple_smile:
👍 1
p

Pavlo Liapota

03/29/2019, 11:51 AM
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

Pascal How

03/29/2019, 11:57 AM
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

karelpeeters

03/29/2019, 1:09 PM
It's frustratingly limited right now, eg. you can't get access to the delegate instance at all.
👍 1
s

sindrenm

03/29/2019, 1:21 PM
That would make sense. It does indeed have its limits, but the idea is good. Thanks!
p

Pascal How

03/29/2019, 1:22 PM
That’s a very good point actually!