Is there any way to access base implementation of ...
# announcements
j
Is there any way to access base implementation of class delegation?
Copy code
interface Base { fun foo(): Int }
object A {
    fun createBase(): Base =
        object : Base { override fun foo(): Int = 1 }
}
object B : Base by A.createBase() {
    override fun foo(): Int {
        // CANNOT access 'super', how to access instance created with 'createBase()' ?
        return super.foo() + 1
    }
}
d
There is no syntax for this when using objects, as far as I know. You'd have to introduce a helper-superclass:
Copy code
interface Base {
    fun foo(): Int
}

object A {
    fun createBase(): Base = object : Base {
        override fun foo(): Int = 1
    }

    abstract class Helper(protected val base: Base) : Base by base

    object B : Helper(createBase()) {

        override fun foo(): Int {
            // CANNOT access 'super', how to access instance created with 'createBase()' ?
            return base.foo()
        }
    }
}
c
if i'm not mistaken, calling super of a delegated super-type is currently not possible. i remember reading this somewhere is the kotlin youtrack
j
Thanks..