sean
07/22/2020, 9:19 PMsuspend
function out of a block and raises this:
But what if the extracted function contains a coroutine builder which is invoked on the current scope? In this case, theIt offers a few options:modifier on the extracted function is not enough.suspend
MakingI agree that it doesn't make the API clearer and usually extensions onan extension method ondoWorld
is one of the solutions, but it may not always be applicable as it does not make the API clearer.CoroutineScope
CoroutineScope
are reserved for things that return immediately like the coroutine builders. Roman offered a great convention around this in a talk or blog post I believe.
The idiomatic solution is to have either an explicitThis makes me 🤔 . Wouldn't a more idiomatic solution be to useas a field in a class containing the target function or an implicit one when the outer class implementsCoroutineScope
.CoroutineScope
coroutineScope
or supervisorScope
from the suspend
function that would 1) respect structured concurrency since the scope that is created would be a child of the scope that the suspend
function was called from and 2) give you a CoroutineScope
that would allow you to call your coroutine builder like launch
, etc.?octylFractal
07/22/2020, 9:21 PMcoroutineScope
would be better, not sure why that isn't suggested, esp. as it is referenced above as wellsean
07/22/2020, 9:29 PMIt is possible to create a new scope without starting a new coroutine. Thefunction does this. When we need to start new coroutines in a structured way inside a suspend function without access to the outer scope, for example inside loadContributorsConcurrent, we can create a new coroutine scope which automatically becomes a child of the outer scope that this suspend function is called from.coroutineScope