https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

Rechee Jozil

04/21/2020, 8:31 AM
What's the easiest and idiomatic way to have Java interop with coroutines. I'd like to call suspend functions somehow in Java. We use our own internal futures library so ideally we'd like some way to take a suspend function and expose it as the future type. And it would be nice to not have to write two functions (one suspend and one that exposes the future). Any suggestions? I took a look at examples in the integrations in repository but not sure if they're updated and the most recent recommendations. @elizarov advice?
a

araqnid

04/21/2020, 9:46 AM
I experimented a bit with calling suspend functions from Java - I used the JDK8 CompletableFuture integration. See here: https://github.com/araqnid/library-versions/blob/master/java/src/main/java/org/araqnid/libraryversions/java/JavaMain.java#L85 It sounds to me like you’d need an equivalent of the JDK8 Futures.kt for your own futures library. The call interface to suspend functions looks a bit odd from Java (with two routes for returning the result), but encapsulating that in a Future of some sort is the way to go.
r

Rechee Jozil

04/21/2020, 3:35 PM
@araqnid Yes exactly I want basically my own version of that Futures library. Thanks for showing me that repo. I noticed you have a static function to call suspend functions which would help eliminate the need to have two separate functions like I thought i needed.
3 Views