How do I call Kotlin functions with lambdas in the parameter from a Java class.
Example
fun getToken(
tokenUrl: String,
onSuccess: (String) -> Unit,
onError: (Error) -> Unit
): Provider {
//Implementation
}
How do I call Kotlin functions with lambdas in the parameter from a Java class.
Example
fun getToken(
tokenUrl: String,
onSuccess: (String) -> Unit,
onError: (Error) -> Unit
): Provider {
//Implementation
}
You can do this
value.getToken("url",
new Function1<String, Unit>() {
@Override
public Unit invoke(String s) {
/* TODO */
return Unit.INSTANCE;
}
}, new Function1<Throwable, Unit>() {
@Override
public Unit invoke(Throwable throwable) {
/* TODO */
return Unit.INSTANCE;
}
});
return Unit.INSTANCE; in the invoke method.You can call it with normal Java 8 lambdas, since Kotlin is actually using a single-method interface on the backend.
myFoo.getToken(tokenUrl, successString -> { ... }, error -> { ... });
This is for calling it within Kotlin, the question was edited to say "from Java" after I provided my answer. Leaving it here anyway in hope that it is useful.
You can call it like this
fun test() {
getToken("asdf", { print(it) }, { println(it) })
getToken("asdf", { print(it) }) { err -> println(err) }
getToken("asdf", ::println, ::println)
getToken("asdf", ::println) { println(it) }
}
Call Kotlin function with Lambda parameters in Java. I think your downvote of my question is unnecessary.