Is Ktor doing it all wrong? Hoping to find some an...
# coroutines
a
Is Ktor doing it all wrong? Hoping to find some answers/opinions on this channel. https://kotlinlang.slack.com/archives/C0A974TJ9/p1640968234268400
s
I think that having a function being both suspend and accepting a CoroutineScope as a parameter (eg extends CoroutineScope), is confusing. This means the function can suspend a while, but in which scope? The calling scope or the provided CoroutineScope? For cancellation, will a suspended call to the function be cancelled when the calling or when the provided CoroutineScope is cancelled? When the suspend function is still busy, will the calling scope or the provided scope wait until it has finished and marked as finished/done? As you can see, it can create confusion and may break Structured Concurrency.
n
It can be confusing to have a method like that. It's not confusing to have a parameter like that (as ktor does in your example). Think about
launch
or
coroutineScope
.
Even kotlinx.coroutines does this with a method for the suspending
stateIn
which launches a coroutine to collect and also suspends until there is a first value.
It's certainly a warning flag that you should recheck your design, but it's not a 100% bad thing.