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

ursus

07/24/2022, 4:43 PM
I'm trying to mimic CoroutineScope in Swift concurrency. My issue is that after I cancel the scope, it no longer accepts new `launch`es Which is good It does how ever still return a
Job
. What is this job? Is it already cancelled or what is it? Wouldn't be cleaner to throw?
z

Zach Klippenstein (he/him) [MOD]

07/24/2022, 5:41 PM
It's valid to try to launch on a cancelled parent job, because it means that your code can just call launch without worrying about race conditions that could occur between that call and something cancelling the parent. Launching a coroutine is a complicated process, the parent job could be cancelled at any point in that process. If it threw instead, there would have to be a mechanism to atomically check if the job was still active, and only launch if it was, and I think that would be very complicated from both implementation and api perspectives - eg probably force every single launch call to be inside a try/catch.
u

ursus

07/24/2022, 5:52 PM
okay so this job instance returned is automatically cancelled right away, then returned?
8 Views