How can I call the extension function when I have ...
# getting-started
k
How can I call the extension function when I have a member function of the same name but taking a differently-typed function parameter?
Copy code
class C {
    fun foo(block: Int.() -> Unit) {
        123.block()
    }
}

fun C.foo(block: String.() -> Unit) {
    "hello".block()
}

fun main() {
    val c = C()
    c.foo {
        println((this as String).length)  // this is Int, want String
    }
}
m
I think you probably have to use an anonymous function instead of a lambda, or do use the trailing lambda and cast the lambda to the correct type
thank you color 1
r
You can get around it with an import:
Copy code
import foo as fooString

class C {
    fun foo(block: Int.() -> Unit) {
        123.block()
    }
}

fun C.foo(block: String.() -> Unit) {
    "hello".block()
}

fun main() {
    val c = C()
    c.fooString {
        println(length)
    }
}
https://pl.kotl.in/yWafqWUx9
thank you color 1