Extension functions issue


#1

Run into some difficulties while using extension functions with existing java api. Here some pseudocode

public class Test {

public Test call() {
    return this;
}

public Test call(Object param) {
    return this;
}

public void configure1() {
}

public void configure2(boolean value) {
}
}

Kotlin test

fun Test.call(toApply: Test.() -> Unit): Test {
return call()
        .apply(toApply)
}
fun Test.call(param: Any, toApply: Test.() -> Unit): Test {
return call(param)
        .apply(toApply)
}
fun main(args: Array<String>) {
val test = Test()
//refers to java method; Unresolved reference: configure1;Unresolved reference: configure2
test.call {
    configure1()
    configure2(true)
}
//refers to my extension function and works fine
test.call(test) {
    configure1()
    configure2(true)
}
}

Why only function with param works fine ? what’s the difference ?


#2

I would say it has something to do with the fact that the Java call method accepts an Object which also includes lambdas, like the one passed in the first test.call {}, and that for extension functions:

A solution would be to change the parameter type of the Java call method.
However, if you are not able to change the Java code I would change the name of the extension to something like invoke and make it an operator (in case it really had something to do with calling something)