@@ -310,32 +310,10 @@ World!
310310But what if the extracted function contains a coroutine builder which is invoked on the current scope?
311311In this case ` suspend ` modifier on the extracted function is not enough. Making ` doWorld ` extension
312312method on ` CoroutineScope ` is one of the solutions, but it may not always be applicable as it does not make API clearer.
313- [ currentScope] builder comes to help: it inherits current [ CoroutineScope] from the coroutine context it is invoked.
314-
315- <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
316-
317- ``` kotlin
318- fun main (args : Array <String >) = runBlocking {
319- launchDoWorld()
320- println (" Hello," )
321- }
322-
323- // this is your first suspending function
324- suspend fun launchDoWorld () = currentScope {
325- launch {
326- println (" World!" )
327- }
328- }
329- ```
330-
331- </div >
332-
333- > You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-05s.kt )
334-
335- <!-- - TEST
336- Hello,
337- World!
338- -->
313+ Idiomatic solution is to have either explicit ` CoroutineScope ` as a field in a class containing target function
314+ or implicit when outer class implements ` CoroutineScope ` .
315+ As a last resort, [ CoroutineScope(coroutineContext)] [ CoroutineScope() ] can be used, but such approach is structurally unsafe
316+ because you no longer have control on the scope this method is executed. Only private API can use this builder.
339317
340318### Coroutines ARE light-weight
341319
@@ -408,7 +386,7 @@ Active coroutines that were launched in [GlobalScope] do not keep the process al
408386[ Job ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html
409387[ Job.join ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html
410388[ coroutineScope ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/coroutine-scope.html
411- [ currentScope ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/current -scope.html
389+ [ CoroutineScope() ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine -scope.html
412390<!-- - END -->
413391
414392
0 commit comments