Is it considered a no-no for class to extend `Coro...
# coroutines
t
Is it considered a no-no for class to extend
CoroutineScope
? e.g.
Copy code
class MyClass : CoroutineScope by CoroutineScope(Dispatchers.Default) {
  fun doSomething() = produce {
    trySendBlocking(...)
  }
}
The objective is to have
MyClass.doSomething
return a ReceiveChannel using
produce
. Extending CoroutineScope seemed to be the most straightfoward way to accomplish this.
z
Yea, it’s discouraged. Put the scope in a private property instead
t
Copy code
private val scope = CoroutineScope(Dispachers.Default)

fun doSomething() = scope.produce { ... }
👍🏻 1
any reason to delegate scope to
lazy
?
z
nope, scopes are cheap
I think this article probably covers the “why?“, although apparently medium is all behind a paywall now so i can’t be sure 😅
t
cool, thanks for the feedback! I’ll change my implementation.
e
@Zach Klippenstein (he/him) [MOD] about the paywall: either pay, or open the article in a private/incognito window to circumvent the paywall 🏴‍☠️