Kotlin: How can I call super's extension function?

Viewed 577

How can I call a super's extension function?

For example:

open class Parent {
    open fun String.print() = println(this)
}

class Child : Parent() {
    override fun String.print() {
        print("child says ")
        super.print() // syntax error on this
    }
}
2 Answers

It is not possible for now, and there is an issue in the kotlin issue-tracker - KT-11488

But you can use the following workaround:

open class Parent {
    open fun String.print() = parentPrint()

    // Declare separated parent print method
    protected fun String.parentPrint() = println(this)
}

class Child : Parent() {
    override fun String.print() {
        print("child says ")
        parentPrint() // <-- Call parent print here
    }
}
Related