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

james

10/25/2019, 4:48 AM
can someone point me to some information on calling a
suspend fun
from a java class with Java 7 compatibility? I found some information by googling on how to do this with Java 8+, but not Java 7
g

gildor

10/25/2019, 4:58 AM
Why do you need this?
In theory it’s possible, but it’s very-very unpleasant to use, and may make sense only for some interop library
Java 8 version also shouldn’t be different from Java 7 (maybe except lambdas)
j

james

10/25/2019, 5:04 AM
CompletableFuture seems to be the way to do it, and it's an Android (pre-API 24) project, so CompletableFuture doesn't exist
s

streetsofboston

10/25/2019, 5:12 AM
Yeah... You'd have to write your own Java-compatible shim/adapter around your API's suspend funs, using Rx or plain callbacks or other mechanisms.
g

gildor

10/25/2019, 5:36 AM
Yes, if you just want to provide way to use coroutines from Java, use any Java library
RxJava, CompletableFuture, ListenableFuture (a part of guava), you can write even own abstraction, if you want and easily write adapter for it
d

Dico

10/27/2019, 2:16 PM
You can call it with reflection if you add the continuation parameter and implement the logic for the return value.
I don't think suspend function names are mangled but based on the answers in the thread I guess you can't just call it normally with the continuation 🤔 Maybe it's difficult to work with the Result type from java as well.