0

For example

open class TestClass

fun TestClass.testFunc(): String = "ext"

now in another file I write:

import xx.xx.testFunc as rename

class OverTestClass : TestClass() {

    fun testFunc(): String {
        return "OverTestClass - " + rename()
    }
}

You can see I rename TestClass.testFunc() as rename() so I can call TestClass.testFunc() in OverTestClass. But this is not cool, what the way I want to is like this:

return "OverTestClass - " + super.testFunc()

Is it possible?

I know this problem is upside down,the reason I want to this is: I am writing a lib used both in java and kotlin, I don't want write java like:

XxxKt.func(obj)

I want to:

obj.func(func)

So I put func as an instance function. and implement directly by its "super" extension function

FredSuvn
  • 1,869
  • 2
  • 12
  • 19
  • Does this answer your question? [extension function in a kotlin using super](https://stackoverflow.com/questions/51170152/extension-function-in-a-kotlin-using-super) – flaxel Sep 06 '20 at 10:35

1 Answers1

2

I'd try up-casting to the TestClass:

class OverTestClass : TestClass() {

    fun testFunc(): String {
        return "OverTestClass - " + (this as TestClass).testFunc()
    }
}
Mafor
  • 9,668
  • 2
  • 21
  • 36