Passing functions to Kotlin Code from Java

I have a Kotlin function that looks like this

calculateFib(upto: Int, fn: (Int) -> Unit): Unit

which I can call from Kotlin like this:

fib.calculateFib(8, ::println)

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

fib.calculateFib(10, System.out::println);

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

So I implement Function1 but the invoke method is pretty ugly

@Override
    public Unit invoke(Object o) {
        System.out.println(o);
        return null;
    }

Is there a better way?

Thanks

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

Thanks

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.