fun CoroutineScope.foo() = launch{//some stuff}
vs
fun foo() = CoroutineScope { launch{//some stuff}}
vs
fun foo(){ coroutineScope { launch { // some stuff } } }
? Thanks
c
CLOVIS
04/11/2020, 4:10 PM
The first reuses an existing CoroutineScope, the second and the third ones create a new one. Not sure what the differences are between those though.
v
viralshah
04/11/2020, 4:11 PM
Is it possible to know which scope is being reused in the first one?
m
Mark Murphy
04/11/2020, 4:16 PM
it is whatever you call
foo()
on
d
Dico
04/11/2020, 6:32 PM
None of these are the same.
1. Is a signature to start a coroutine and return immediately, with structured concurrency pattern.
2. I am not sure this even compiles.
3. Needs to be
suspend
, creates a derivative coroutine scope and doesn't return until all children of that scope have died.
r
rkeazor
04/11/2020, 11:32 PM
The 3 one Creates a CoroutineScope that inherits the context that was used at the call site. Its good to use in classes that don't have a coroutineScope. Simiarly the first one is a extension function, of CoroutineScope, so you will have access to things like launch within that function. . Never seen the second one before 😅