molikuner
08/13/2019, 6:36 PMfun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
for (x in 1..5) send(x * x)
}
What I would expect:
suspend fun produceSquares(): ReceiveChannel<Int> = produce {
for (x in 1..5) send(x * x)
}
Later in the document you can find:
All functions that create coroutines are defined as extensions on CoroutineScope, so that we can rely on structured concurrency to make sure that we don't have lingering global coroutines in our application.But doesn't the
suspend keyword does exactly that? It makes the current CoroutineContext implicitly available to the called function?octylFractal
08/13/2019, 6:39 PMCoroutineContext != CoroutineScope. Scope is generally used to start new coroutines, like with produce. It's hard for me to word the exact reason why suspend doesn't implicitly have it available, but it has to do with structured concurrency. If you want a CoroutineScope in suspend functions, you typically use the coroutineScope functionmolikuner
08/13/2019, 6:47 PMZach Klippenstein (he/him) [MOD]
08/14/2019, 6:03 PMmolikuner
08/14/2019, 6:04 PM