Hello, everyone. I'm writing a KEEP to improve int...
# language-proposals
d
Hello, everyone. I'm writing a KEEP to improve interface delegation in Kotlin. Summary of the problem: Currently, interface delegation is unnecessarily limited in a number of ways: • Delegate expressions are evaluated once, on construction, no exceptions. • On the JVM, the result of the delegate expression is kept in an invisible field, it cannot be accessed. • Changing the behaviour of individual methods requires delegation to a constructor parameter to access the delegated instance. • Delegate expressions cannot refer to
this
instance at all. It is obviously not feasible to replace existing behaviour with a new one - where the expression is evaluated on each call. Thus, a new syntax needs to be come up with to distinguish between the two. Current ideas, where B is an interface: •
Copy code
class A : B by val foo { 
    val foo : B = ...  
}
Copy code
class A : B {
    val foo : B = ...
    delegate B to foo
}
Copy code
class A : B by ::foo {
    val foo : B = ...
}
Copy code
class A : B {
    @Delegates(B::class)
    val foo: B = ...
}
Please share any ideas you have for a better syntax, or why you think one is better! The behavioural difference between the old and new syntax must be obvious. I am also considering proposing a compiler-intrinsic
inline fun <reified T : Any> delegate(): T
function which would simply emit code that gets the delegate used for the given type under the hood (for delegating methods). There are some issues with this declaration - would love to hear opinions. Have a look at [KT-83](https://youtrack.jetbrains.com/issue/KT-83) for background. EDIT: In [this gist](https://gist.github.com/Dico200/0065293bcd9a19c50e371cde047a9f22) we can discuss and make changes to the KEEP before it's submitted as a PR in the [KEEP repository](https://github.com/Kotlin/KEEP)
👍 1