https://kotlinlang.org logo
Title
y

yschimke

03/06/2022, 9:57 AM
How far can you get with the coroutines classes in stdlib, without kotlinx.coroutines? From https://stackoverflow.com/questions/58237116/kotlin-coroutines-possible-without-standard-library it seems technically possible but likely a dangerous path. But for an existing callback API with internal threading, is there a viable path to implement a suspend function without kotlinx.coroutines? It would also need to support cancellation in both directions.
j

Joffrey

03/06/2022, 10:01 AM
Why are you trying to avoid the kotlinx.coroutines library?
y

yschimke

03/06/2022, 10:19 AM
I'm not totally, will use it outside core modules, but wondering whether I can have a suspend function in the core module without pulling in kotlinx.coroutines for everyone (Java apps on the JVM that may not use kotlin or kotlin coroutines). Not using it at all internally, it's basically a nice to have for a public API.
If not, then I'll just provide some extension methods, but thought it worth checking. The last SO answer suggests it is possible.
I guess the parallel here is guava-listenable future. It's just enough to use ListenableFuture in a library API, without bringing in Guava in it's entirety. It's still hard to actually implement that method because you need an ListenableFuture implementation. Androidx has a private implementation that shows it's possible but not simple. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:concurre[…]c/main/java/androidx/concurrent/futures/ResolvableFuture.java
Seems similar here, suspendCoroutine is in stdlib, but doesn't seem to support cancellation.
j

Joffrey

03/06/2022, 10:45 AM
AFAIK you could use
suspendCoroutine
without kotlinx.coroutines, but if you want cancellation you might need the kotlinx.coroutines library. The structured concurrency with Jobs and cancellation is all defined in the coroutines library, so it would make sense to not have related helpers in the stdlib
👍🏻 1
y

yschimke

03/06/2022, 10:47 AM
Thanks, makes sense, I'll stick with clean extension methods outside of core.