dagguh
05/10/2018, 1:55 PMNote, 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 membersThis behavior is the opposite of what a manual delegation pattern does. What’s the rationale behind this difference?
Andreas Sinz
05/10/2018, 2:05 PMdagguh
05/10/2018, 2:16 PMinterface 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()
}
print
in ManualDelegate
, I’ll get the same behavior as in KotlinDelegate
Andreas Sinz
05/10/2018, 2:31 PMManualDelegate
and KotlinDelegate
(apart from another()
implementation). am I missing something?dagguh
05/10/2018, 2:32 PMprint
, but Kotlin doesAndreas Sinz
05/10/2018, 2:34 PMManualDelegate
it prints ManualDelegate
and inside KotlinDelegate
it prints e. g. Base
?dagguh
05/10/2018, 2:36 PMBase
Base
Base
Inherited
ManualDelegate
Andreas Sinz
05/10/2018, 2:39 PMdelegate
calls something from KotlinDelegate
, because there is no inheritance involved and delegate
doesn't know about the other classdagguh
05/10/2018, 3:12 PM