Kotlin: When extension function hides the class default implementation?

Viewed 237

I'm trying to figure out when overriding an existing class function, with an extension function of the same signature - will take effect?

Here is my sample code:

fun String.toUpperCase(): String = "ext. function impl."

fun main(args: Array<String>) {

    println("Hello".toUpperCase()) // ext. function impl.

    println(MyClass().toUpperCase()) // class impl.

}

class MyClass {
    fun toUpperCase() : String {
        return "class impl."
    }
}

fun MyClass.toUpperCase() : String {
    return "ext. function impl."
}

So:

  • What are the rules? when each will be called?
  • How can I override this decision? is it possible?
1 Answers
Related