Passing functions to Kotlin Code from Java


I have a Kotlin function that looks like this

which I can call from Kotlin like this:

I’m trying to call this from Java and hoped I could do this:

but I get a ‘Found method reference expected Function1’ error

So I implement Function1 but the invoke method is pretty ugly

    public Unit invoke(Object o) {
        return null;

Is there a better way?



Unfortunately there isn’t at the moment. There is an open issue covering the use of Java method references with Kotlin functional types.




The only workable solution I could come up with would be to generate the function (that takes the funcion parameter) twice, one that takes a unit returning function, one that takes a void function. This would have to be generated twice unless the compiler can decide that the unit return value is always discarded, in which case the code could be shared.