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

jcechace

10/29/2018, 7:26 PM
In roman’s talk at Kotlin conf it is recommended to define function which are a coroutine as an extension on
CouroutineScope
what if the coroutine is a method inside class? How is this going to behave?
Copy code
class Watcher {
    fun CoroutineScope.watcherStarted() = async {

    }
}
b

bdawg.io

10/29/2018, 9:43 PM
That method can only be invoked within the context of a
Watcher
. So one way you can do this:
Copy code
...launch {
    val watcher = Watcher()
    watcher.run {
        watcherStarted() // invokes the CoroutineScope extension from launch
    }
}
3 Views