Adriano Celentano
10/27/2019, 12:28 PMsuspend fun test() {
suspendingFunctionScope.launch {
...
}
}
Fudge
10/27/2019, 12:35 PMtseisel
10/27/2019, 12:36 PMlaunch
call into a coroutineScope
block:
suspend fun test() = coroutineScope {
launch {
...
}
}
Note that the test
function will not return before the launched coroutine is completed. If that's not what you want, you should consider making test
a non-suspending extension of CoroutineScope
, which is the usual way to define a function that starts a coroutine:
fun CoroutineScope.test()
Adriano Celentano
10/27/2019, 12:48 PMclass GameEngine {
fun CoroutineScope.start() {
...
}
tseisel
10/27/2019, 1:06 PMval engine = GameEngine()
with(engine) {
scope.start()
}
This is because a member function can (normally) have only one receiver.
Something like fun startIn(scope: CoroutineScope)
would be better. You can also pass a CoroutineScope
as a constructor parameter of your engine, provided the engine has the same lifetime as the passed scope.Adriano Celentano
10/27/2019, 1:56 PM