This question is about the parameter callback in Kotlin, which is by the way a very nice feature from my point of view!
I have a method written in Kotlin like this one which expects a callback as argument. The callback itself expects a String argument, which should be given to the callback invocation reciever:
`private fun m1(number: Int, callback: (result: String) -> Unit) {
//some other stuff..
val string = "Foo"
callback.invoke(string)
}`
Then usually I would use it in Kotlin like this way:
m1(101) { processResult(it) } Whereas it the actual result is
BUT... how to get and process the callback result if the caller of the method is a Java class? I tried something like this one but it does not work:
`m1(101, () -> processResult(result));`
Thanks for any help! See you later.
Here is how you need to call higher order function in java.
With Lambda:
Without Lambda:
You can see the invoke function above. It returns Unit. Unit is similar to void in Java. Kotlin takes the default value. But in java you need to return Unit instance. You can
return nullorUnit.Instance. Both will be working as it would do nothing with the return value here. HereFunction0is an interface which takes zero argument in invoke method. Similary if you higher order function contains 1 argument, you need to useFunction1and so on.