https://kotlinlang.org logo
Title
m

Marko Mitic

07/23/2019, 2:02 PM
what's the closest thing in java to
suspend fun getAThingThatMayTakeTime() : T
?
fun getAThing(): Future<T>
?
j

jw

07/23/2019, 2:02 PM
More like CompletableFuture or ListenableFuture or RxJava's Single
1
d

Dominaezzz

07/23/2019, 2:04 PM
fun getAThing(cont: Continuation<T>)
😛
m

Marko Mitic

07/23/2019, 2:13 PM
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

Dominaezzz

07/23/2019, 2:19 PM
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

diesieben07

07/23/2019, 2:21 PM
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

Dominaezzz

07/23/2019, 2:22 PM
Do you have any examples?
d

diesieben07

07/23/2019, 2:23 PM
suspend fun doStuff() {

}

fun doStuffJava() = GlobalScope.future { doStuff() }
This way both Kotlin and Java can call it idiomatically
m

Marko Mitic

07/23/2019, 2:24 PM
Yeah, I'm trying to let java call my suspend API (indirectly)
d

diesieben07

07/23/2019, 2:24 PM
future
(and other helpers) are the way to go then
m

Marko Mitic

07/23/2019, 2:24 PM
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

Dominaezzz

07/23/2019, 2:26 PM
What about interfaces?
d

diesieben07

07/23/2019, 2:26 PM
What about them?
m

Marko Mitic

07/23/2019, 2:27 PM
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

diesieben07

07/23/2019, 2:27 PM
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

Dominaezzz

07/23/2019, 2:28 PM
How do you provide an adapter without repeating yourself?
d

diesieben07

07/23/2019, 2:28 PM
I don't think you can. You have to explicitly design for Java interop with coroutines
😞 1
d

Dominaezzz

07/23/2019, 2:28 PM
Guava comes with androidx. In some form.
j

jw

07/23/2019, 2:30 PM
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

Marko Mitic

07/23/2019, 2:34 PM
blocking call it is, thank you everybody