As exxplained here you can declare extension functions as members and even override them in subclasses. So take for example:
interface Foo {
fun Bar.bar() : Unit
}
open class BaseClass : Foo {
override fun Bar.bar() {
// Make common calls on bar instance
}
}
class SubClass : BaseClass() {
override fun Bar.bar() {
// How can I call the super class implementation?
// Make subclass specific calls on bar instance
}
}
As the comment asks, I want in the subclass override of the extension method to call the implementation in the super class, but I cannot find any valid syntax to accomplish it.
The template generated from overriding the method generates super.bar()
, which is not valid. Thought this might work, but no luck since super is not a value:
with(super) { this@bar.bar()}
Still unable to find any way to pull it off. Is it possible in Kotlin to somehow reference the superclass implementation of the extension method being overridden??? If not, this seems like a whole in the language.
Passing Bar as a parameter is unacceptable. My only work around right now is to switch to a property as in:
interface Foo {
val bar: Bar.() -> Unit
}
open class BaseClass : Foo {
override val bar: Bar.() -> Unit = {
// Make common calls on bar instance
}
}
class SubClass : BaseClass() {
override val bar: Bar.() -> Unit = {
super.bar.invoke(this)
// Make subclass specific calls on bar instance
}
}