what's the closest thing in java to `suspend fun g...
# coroutines
m
what's the closest thing in java to
suspend fun getAThingThatMayTakeTime() : T
?
fun getAThing(): Future<T>
?
j
More like CompletableFuture or ListenableFuture or RxJava's Single
1
d
fun getAThing(cont: Continuation<T>)
😛
m
Followup: is there any easy way to wrap suspend call into Future/ListenableFuture (on Android)
is it safe to use on Android with limited java 8 api?
I'll assume it's not 😄
d
From java or from kotlin?
Are you trying to allow java call your kotlin suspend function?
If you want to explicitly support java, don't bother with suspension.
Just use
GlobalScope.future
.
d
Nothing wrong with using suspension in your kotlin code, that's what the adapters like
future
, etc. are for
You can write normal suspend functions and then adapter methods for calling from Java
d
Do you have any examples?
d
Copy code
suspend fun doStuff() {

}

fun doStuffJava() = GlobalScope.future { doStuff() }
This way both Kotlin and Java can call it idiomatically
m
Yeah, I'm trying to let java call my suspend API (indirectly)
d
future
(and other helpers) are the way to go then
m
I don't see
GlobalScope.future
, what dependency do I need for that?
There is also one for guava, giving a wrapper for ListenableFuture
d
What about interfaces?
d
What about them?
m
I don't have full Java8 API on android, I don't think I can use
kotlinx-coroutines-jdk8
, I'll try implementing something myself
d
You do need some kind of future if you want to use it from Java
If you can't use Api level 24 (not quite sure how this works on Android) then you should use Guava
d
How do you provide an adapter without repeating yourself?
d
I don't think you can. You have to explicitly design for Java interop with coroutines
😞 1
d
Guava comes with androidx. In some form.
j
It does not
There is a ListenableFuture artifact which provides a SettableFuture that you can use
Definitely do not expose just a Future. It is an entirely useless type.
6
m
blocking call it is, thank you everybody