Davide Giuseppe Farella
01/29/2019, 10:20 PMclass MyClass {
fun CoroutineScope.run()
}
MyClass
become the context of run()
, while CoroutineScope
become the "owner".
That makes sense... But what I want the opposite effect?
Sometimes I use this pattern
class Mapper<E, P> {
fun <T> invoke( block: Mapper.() -> T ): T
fun E.toPojo(): P
fun P.toEntity(): E
}
And makes sense that Mapper
is the context for call mapper { entity.toPojo() }
But back to the first example, doesn't make sense to call
launch {
with( myObj ) { run() }
}
I would simply call
launch {
myObj.run()
}
Is that possibile?ghedeon
01/29/2019, 10:49 PMwith( myObj ) { run() }
, or you pass the scope as a parametr and do myObj.run(scope)
.cancellableCoroutine
or yeld
. I'm curious to see some examples.Davide Giuseppe Farella
01/29/2019, 10:55 PMghedeon
01/29/2019, 10:56 PMCoroutineScope
in all your public methods, later you refactor and realize that for a particular case suspend
is enough and scope
param is redundant? How many places do you need to change now? 🤔 What if you change your mind the next day?Davide Giuseppe Farella
01/29/2019, 11:06 PMsuspend
everywhere, but in this case I don't care about the "lifecycle" of this function, I just care about results returned by `Channel`ssuspend operator fun <T> MyClass.invoke( block: suspend MyClass.() -> T ) = block()
launch { myObj { doSmtng() } }
Mapper
above 😔