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

Tucker Barbour

05/24/2021, 3:27 PM
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

Zach Klippenstein (he/him) [MOD]

05/24/2021, 3:28 PM
Yea, it’s discouraged. Put the scope in a private property instead
t

Tucker Barbour

05/24/2021, 3:31 PM
Copy code
private val scope = CoroutineScope(Dispachers.Default)

fun doSomething() = scope.produce { ... }
👍🏻 1
any reason to delegate scope to
lazy
?
z

Zach Klippenstein (he/him) [MOD]

05/24/2021, 3:34 PM
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

Tucker Barbour

05/24/2021, 3:38 PM
cool, thanks for the feedback! I’ll change my implementation.
e

Erik

05/24/2021, 4:01 PM
@Zach Klippenstein (he/him) [MOD] about the paywall: either pay, or open the article in a private/incognito window to circumvent the paywall 🏴‍☠️
2 Views