How do I access the delegating instance when using...
# getting-started
z
How do I access the delegating instance when using delegated inheritance. Is it possible?
Copy code
interface Validatable {
    fun validate()
}

class Impl() :
    // both these extend `Validatable`
    Validatable1 by Validatable1.Impl(),
    Validatable2 by Validatable2.Impl(),
    Validatable {

    override fun validate() {
        // how do I call:
        // validatable1.validate()
        // validatable2.validate()
        
        // or bonus - can I access the `Validatable1.Impl()` instance at all?
    }
}
e
to access them, you need to give them a name
Copy code
class Impl(
    val validatable1: Validatable1,
    val validatable2: Validatable2,
) : Validatable1 by validatable1, Validatable2 by validatable2 {
    constructor() : this(
        validatable1 = Validatable1.Impl(),
        validatable2 = Validatable2.Impl(),
    )

    override fun validate() {
        validatable1.validate()
        validatable2.validate()
    }
}
👍 1
there is a proposal to reuse the
super<T>.function()
syntax that is already available for choosing between default implementations coming from multiple interfaces, but it doesn't look like it's gotten any attention https://youtrack.jetbrains.com/issue/KT-7786/Make-it-possible-to-call-method-from-delegate-using-superMyDelegateInterface-qualifier
a
One suggestion , Please do not do this , this is pure inheritance abuse .