I don't find Kotlin class delegates very useful. I...
# android
s
I don't find Kotlin class delegates very useful. I've found many uses for property delegates, but I have never found the class delegation in Kotlin to be what I needed. I've been programming in Kotlin for almost 4 years, and haven't used it once. Has anyone found it to be useful, and can they explain how they use it?
d
I also have a hard to finding a good use for this feature. In my case, its turns out that this feature is really a form of the Decorator pattern instead of just a plan delegate pattern, which is why the class thats using
by
must also conform to the interface. If there was a similar kotlin feature like this that did not implement/conform the interface methods in the class using
by
, I would be using this like crazy. The current way it works comes in handy for places where you would use the decorator pattern.
p
Yes it is extremely useful for example by lazy delegate which does object initialization when its used for the first time thereafter directly use its value. Observer/Vetoable are great way to observer value and restrict what value should be assigned to an object.
lazy initialization make your object immutable that means no overhead included to allocate space for variable memory as its value never change
t
I typically only use it as a way to reduce code duplication around things like rx disposable management or viewholder code where a custom view isn’t any more appropriate. ie)
Foo(...): DisposeBucket by DisposeBucket.Impl()
👍 1
k
Why wouldn't you use it for class composition?
r
I found it useful
It favors composition over inheritance. I think the problem most people have , is that they are used to the inheritance model so its can be somewhat weird to wrap your head around
👍 2
in essence you can delegate the actual implmentation of Object. so for instance, in a normal MVP(passive view flavor) architecture you would have a interfaces that seperate the layer of abstraction between the presenter and Activity. However you would have to have the actual implementation as a instance variable
in both the activity and the presenter. but class delegation removes this need
The only downside I see with it , was perhaps the same downside you get from Multiple inheritance, and default methods..Is that if you do it to much you don’t know where things are coming from lmaoo
but i think its a cool feature
s
Every time I use it, I find it's limitations crippling. I've seen 2 uses for it 1. When I want to delegate all methods from an interface to another class, but then I'm asking myself why am I not just extending the class, since this 95% of the time, the class definitely won't be extending anything else. I agree learned to favor composition over inheritance, but the limitations of the class delegate in Kotlin have ensured that I can't use it for that in all the instances I've come across. 2. If I split an interface into multiple smaller interfaces, and then kind of patched implementations together of the implementations using the class delegates. I could see it useful for this, effectively using this as a decorator pattern of some sort. I'm not sure if this would actually work well though. I'm sure it is useful, I just haven't seen a way that I've found it to be useful. I'm confident that if someone showed me a real example of how its been useful for them it would probably grow me as a programmer, as I think it's likely there is just a mental block there now.
r
yea you can extend it, but remember Java and Kotlin favor single Inheritance.So if you have a class that extends a base class, and you want to inherit from another class as well, you will have to create a instance variable of that class
l
I found a very compelling use case for it on Android that also involves companion objects. I'll share it back here this week (or maybe today)
Here's what you can do with interface implementation delegates, companion objects, and reified type parameters for Android
s
?
l
Oh… silly me, forgot the link… Here it is: https://github.com/LouisCAD/Splitties/blob/master/intents/README.md @spierce7 Feel free to jump straight to the example