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.

1 Answer 1

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; } ); 
Sign up to request clarification or add additional context in comments.

1 Comment

and if the kotlin function does return something, how can i access this return by using the "FunctionalUtils" helper method from @KirillRakhman?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.