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

scottiedog45

11/03/2019, 3:22 PM
What’s the difference between these two syntaxes?
Copy code
val scope = CoroutineScope(errorHandler)
scope.launch {
    throw SomeSillyException()
}

val scope = CoroutineScope()
scope.launch(errorHandler) {
    throw SomeSillyException()
}
d

Dominaezzz

11/03/2019, 3:43 PM
Iirc
errorHandler
is notified and that's it.
s

scottiedog45

11/03/2019, 3:43 PM
in both cases the errorHandler would be notified
d

Dominaezzz

11/03/2019, 3:45 PM
There's no real difference. The latter is just more explicit.
s

scottiedog45

11/03/2019, 3:47 PM
It makes sense - thanks 👍🏻
s

streetsofboston

11/03/2019, 4:35 PM
I prefer the
CoroutineScope(errorHandler)
variation, since every
launch
from that scope will report its unhandled exceptions in the handler. For the second variation, only for that one particular
launch
will report its unhandled in the handler...
e

elizarov

11/04/2019, 9:46 AM
The effective parent context is the union of the scope’s context and the explicitly passed one, so there is not behavioral different difference here. https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055
s

scottiedog45

11/04/2019, 7:14 PM
Got it - thanks everyone for weighing in. I’m trying to fine tune my coroutine composition skills
3 Views