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

azabost

06/22/2022, 7:40 PM
Hey. In general, do I need to include a dependency on the coroutines library in a module to write a
suspend fun
? I’m asking about it only because I’m trying to optimise some modules’ dependencies (e.g. get rid of unnecessary
implementation
/
api
dependencies)
e

ephemient

06/22/2022, 7:46 PM
a basic
suspend fun
may not need any external dependencies; the compiler and stdlib contain core coroutines functionality. but many things you may want to do in a suspending function will depend on the kotlinx.coroutines library
n

Nick Allen

06/22/2022, 8:43 PM
What's available in stdlib is listed here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/ There's no
CoroutineScope
Job
launch
delay
CoroutineDispatcher
.
a

Adam Powell

06/22/2022, 11:37 PM
The lack of
suspendCancellableCoroutine
is going to mean that any suspend functions you write this way won't be cancellable either (unless you're calling something else that brings in kotlinx.coroutines as a transitive dependency)
e

ephemient

06/22/2022, 11:41 PM
or perhaps you write a higher-order function - if invoking a given cancellable suspend fun, it should be cancellable - but yeah. in order to properly bridge the blocking and suspending worlds in a way that cooperates with kotlinx.coroutines, you need kotlinx.coroutines
1
a

azabost

06/24/2022, 6:02 AM
In my case I wanted to have an interface with a suspend fun method without any implementation in that module. Thanks for all your answers
a

Adam Powell

06/24/2022, 2:07 PM
Sure, that would work. I'd expect that module to grow and need it before too long though; callers expecting cancellability of that interface method is going to drive quite a bit
5 Views