elizarov
01/29/2019, 7:19 AMdoSomething to do:
• If you want to wait until it finishes doing its work you write suspend fun doSomething() = coroutineScope { ... }
• If you want it to return quickly with a side effect of launching new coroutine, you write fun CoroutineScope.doSometing() = launch { ... }.svenjacobs
01/29/2019, 7:35 AMclass EventBus {
fun CoroutineScope.send(...) { ... }
}
// somewhere else
val eventBus = EventBus()
launch {
eventBus.send(...)
}svenjacobs
01/29/2019, 7:36 AMclass EventBus {
fun send(scope: CoroutineScope, ...) { ... }
}Ruckus
01/29/2019, 7:37 AMsvenjacobs
01/29/2019, 7:38 AMEventBus to work?Ruckus
01/29/2019, 7:39 AMval eventBus = EventBus()
with(eventBus) {
send()
}
Sorry, I fixed my copy/paste errorsvenjacobs
01/29/2019, 7:42 AMwith() works but not without. Why is that?Ruckus
01/29/2019, 7:43 AMEventBus to use extension methods declared in it. It's a scoping issue, not specific to coroutines.svenjacobs
01/29/2019, 7:45 AMeventBus. I thought everything after the . is then called in the context of EventBus. So I'm calling the function on the object. I wonder if this should work for extension function of the object, too?svenjacobs
01/29/2019, 7:52 AMCoroutineScope, why shouldn't the compiler be able to do this
eventBus.send(...)
and pass the CoroutineScope as the first function argument during compile time.Ruckus
01/29/2019, 7:56 AMeventBus.foo() calls foo on eventBus not in the context of eventBusRuckus
01/29/2019, 7:58 AMthis points to the thing. so in
with (eventBus) {
send()
}
You're calling send() on the CoroutineScope, but the call is being made in the context of eventBus.streetsofboston
01/29/2019, 12:21 PMinContext function below.
class MyClass {
fun printValue(value :Int) {
println("Value ${value.inContext()}")
}
fun Int.inContext(): String {
return (this + 6).toString() + this@MyClass.hashCode().toString()
}
}
fun main() {
MyClass().printValue(4)
}Ruckus
01/29/2019, 2:30 PM