0

I have a library written in kotlin which i want to use in my Java program, but i don't know how to call an asynchronous method correctly.

/*This is my asynchronous kotlin method from the library*/

fun getNames(callback: (List<String>) -> Unit) = ...

/*In Java i want to call this method*/

List<String> names = getNames(...);

What parameter do i have to pass into the function in Java? IntelliJ says i need something like this: 'kotlin.jvm.functions.Function1<? super java.util.List<String>,kotlin.Unit>' But i don't know how to instantiate such a class.

knorrbert
  • 100
  • 9

1 Answers1

0

Ok, first of all give it a look at this great response from Kirill Rakhman.

Now, the result given by a callback from a asynchronous operation will come from the parameter. In this case it'll be the (List<String>) at the fun getNames().

A simple usage of this function in JAVA (8+) can look like this :

getNames(
    strings -> {
        strings.forEach(System.out::println);
        return Unit.INSTANCE;
    }
);
csar
  • 131
  • 4
  • and if the kotlin function does return something, how can i access this return by using the "FunctionalUtils" helper method from @KirillRakhman? – knorrbert Sep 04 '19 at 21:08