How would I achieve the following? ```open class A...
# getting-started
m
How would I achieve the following?
Copy code
open class A {
    open fun C.extMethod() { ... }
}

class B : A() {
    override fun C.extMethod() {
        // Somehow call A's implementation
        // Then do extra behavior
    }
}
super.extMethod(this)
doesn't seem to resolve unfortunately
j
Looks like it's currently not possible: https://youtrack.jetbrains.com/issue/KT-11488 There is a workaround at the end of this, I experimented and it seems to work, but it requires creating a separate class function to expose the extension behavior:
Copy code
interface Foo {
    fun Bar.bar()
}

open class BaseClass : Foo {
    open fun doSomethingWithBar(bar: Bar) {
        println("parent impl")
    }

    override fun Bar.bar() = doSomethingWithBar(this)
}

class SubClass : BaseClass() {
    override fun Bar.bar() {
        super.doSomethingWithBar(this) // now works
        println("subclass")
    }

    override fun doSomethingWithBar(bar: Bar) {
        bar.bar()
    }
}

fun main(args: Array<String>) {
    val theClass = SubClass()
    theClass.doSomethingWithBar(Bar())
    // parent impl
    // subclass
}
s
oh course, you could always refrain from making
extMethod
an extension method, by moving the context C to the first parameter of the method.
Copy code
open class A {
    open fun method(c: C) { ... }
}

class B : A() {
    override fun method(c: C) {
        // call A's implementation
        super.method(c)
        // Then do extra behavior
    }
}
You probably knew that, so i mention this just for the sake of completeness.