Suggestions for Interface delegation


I already talked about interface delegation in this topic, and other people have also noted the limitation of the way it’s currently implemented.
The purpose of this topic is to draw your attention to the problem once more and to show you a few suggestions for fixing the issue.

To briefly recap the problem, you can’t currently assign a visible property to be used as the delegate.
Instead, the expression you give is evaluated once on instantiation and kept in an invisible field.

Suggestion #1

class A : B {
    val a: B
    implement B by a


+ declaration and assignment of a delegate is split
+ assignment of a delegate occurs inside the class brackets
+ can use property getters to have an expression that is evaluated each time
- potentially a little bit confusing because it looks very similar to the current syntax outside of the class, but behaves very differently.
- implement is not a keyword: it would have to become soft keyword.


a implements B
I don’t think this one is good because it can be understood as saying that a is of type B or adding a type constraint to a, which isn’t the intention here.

B implemented by a
Basically saying the same thing in a passive construct

Suggestion #2

class A(val del: B) : B by { del }


+ (fixes the problem)
+ versatile
- ugly (imo)
- interferes with ability to implement Function0, although I have never seen that use case

+ need the ability to refer to this