Hey guys, whats the difference between ```fun Coro...
# coroutines
v
Hey guys, whats the difference between
Copy code
fun CoroutineScope.foo() = launch{//some stuff}
vs
fun foo() = CoroutineScope { launch{//some stuff}}
vs
fun foo(){ coroutineScope { launch { // some stuff } } }
? Thanks
c
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
Is it possible to know which scope is being reused in the first one?
m
it is whatever you call
foo()
on
d
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
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 😅