<https://kotlinlang.org/docs/reference/delegation....
# announcements
d
https://kotlinlang.org/docs/reference/delegation.html
Note, however, that members overridden in this way do not get called from the members of the delegate object, which can only access its own implementations of the interface members
This behavior is the opposite of what a manual delegation pattern does. What’s the rationale behind this difference?
a
how do you implement that with manual delegation?
d
Copy code
interface Named {
        fun name(): String
        fun another()
        fun print() {
            println(name())
        }
    }

    open class Base : Named {
        override fun name() = "Base"
        override fun another() { println("Some complex behavior") }
    }

    class KotlinDelegate(private val delegate: Named) : Named by delegate {
        override fun name(): String = "KotlinDelegate"
    }

    class Inherited : Base() {
        override fun name() = "Inherited"
    }

    class ManualDelegate(private val delegate: Named) : Named {
        override fun name(): String = "ManualDelegate"
        override fun another() = delegate.another()
    }
However, I see the difference is only there when I use default interface methods
If I manually delegate
print
in
ManualDelegate
, I’ll get the same behavior as in
KotlinDelegate
a
i don't see a difference between your
ManualDelegate
and
KotlinDelegate
(apart from
another()
implementation). am I missing something?
d
yeah, I’m not delegating
print
, but Kotlin does
I’ll stick to the Kotlin way and rearrange my code so this doesn’t become a problem
a
Ah, that means inside your
ManualDelegate
it prints
ManualDelegate
and inside
KotlinDelegate
it prints e. g.
Base
?
d
exactly
Copy code
Base
Base
Base
Inherited
ManualDelegate
a
technically there is no way that
delegate
calls something from
KotlinDelegate
, because there is no inheritance involved and
delegate
doesn't know about the other class
so your main problem is that kotlin automatically implements default methods when delegating
d
my original problem was: the calls are early-bound, e.g. not late-bound from the top-level implementor I no longer consider this a Kotlin delegation problem, because it’s just how Java works
I’m only able to get the “late-bind” via default method or inheritance