I have seen few similar questions, but none had explained why delegation is limited to interfaces?
Most of the time in practice we have something that has actually no interface at all, it is a class that implements nothing but provides some functionality or implements an abstract class.
Is there any fundamental limitation that forces this to be limited to interfaces or can we expect kotlin to have unrestricted delegation in the future?
This is especially useful if we want to extend functionality of a class using composition not inheritance.
class A {}
class B(val a: A) : A by a {}
open
orabstact
) should be delegated automatically. 2. SoB
should be required to pass the constructor parameters up toA
as usual. 3. The Kotlin authors already had to make this decision for interface delegation since every interface implicitly extendsAny
and thus hasequals
,hashCode
andtoString
. – Lister