Declaring ``` class MyClass { fun CoroutineSco...
# announcements
d
Declaring
Copy code
class 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
Copy code
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
Copy code
launch {
    with( myObj ) { run() } 
}
I would simply call
Copy code
launch {
    myObj.run()
}
Is that possibile?
Basically yes, you either do
with( myObj ) { run() }
, or you pass the scope as a parametr and do
myObj.run(scope)
.
@Allan Wang mentioned some alternatives, tho. With
cancellableCoroutine
or
yeld
. I'm curious to see some examples.
d
Ops 😅 I hoped in a fancier way to do that 😁
g
aren't we all? 🧌
👍 1
it's all shine and roses when the caller is close to the fun definition. But if you start thinking of deeper callstack, through your domain, data layer... ugh. Let say you put
CoroutineScope
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?
d
Usually I use
suspend
everywhere, but in this case I don't care about the "lifecycle" of this function, I just care about results returned by `Channel`s
That's my use case
The best solution I found as now
Copy code
suspend operator fun <T> MyClass.invoke( block: suspend MyClass.() -> T ) = block()
launch { myObj { doSmtng() } }
Basically the same pattern described for
Mapper
above 😔