I'd like to be able to write `val a by delegate1(...
# announcements
c
I'd like to be able to write
val a by delegate1() and delegate2()
I'm expecting it to have to define
infix fun Delegate.and(other: Delegate): Delegate {...}
But since delegation is defined on the presence of methods, and not on the inheritance from an interface, I don't know how to do it :/
n
you might need to write
val a by (delegate1() and delegate2())
and then the returned object/instance from
and
just needs to have the correct functions for delegation
👍 3
c
@Nikky If the function ‘and' takes two
Any
, then I can't call
getValue
on the objects to combine them
And since there's no interface for delegates, I can't put anything else than
Any
?
t
just to check if I understood right: you're trying to create an extension function that can somehow combine any 2 delegates and your problem is that delegates don't need to implement a specific interface you could use as a receiver/parameter
c
Yes, exactly
I guess it's possible to cheat and implement this without combining delegates, I'm mostly doing this out of curiosity to what is possible and what isn't
t
I don't think there is any way to do exactly that in kotlin (I guess it would require something like implicit classes or C-style generics). If you only need to use this with delegates you wrote yourself, you could use
ReadOnlyProperty
and
ReadWriteProperty
interfaces, but most default delegate implementations don't extend those.
👍 1
c
Hm.
I don't understand why the language designers chose not to use inheritance here, it seems weird that that's the only place in the language that doesn't take advantage of it
t
well, it's not really the only place. basically all operator functions work without interfaces
n
C-style generics?
C++ style I guess?
t
sure, sorry