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

df

11/29/2020, 4:56 PM
What is the correct way to define a method that returns a ReceiveChannel<E>?
Copy code
class SomeClass { fun CoroutineScope.producer() = produce { ... } }
or is it
Copy code
class SomeClass { fun producer(scope: CoroutineScrope) = scope.produce { ... } }
or should (for some reasons) those methods never be methods but just functions? If it's the first one, do I really need to invoke it via
Copy code
with(obj) { producer() }
? If it's second one, do I just want to pass in
this
? Might sound random but I still find myself frequently in a situation where I know how something can be done but never why.
z

Zach Klippenstein (he/him) [MOD]

11/30/2020, 2:02 PM
I would probably go with the latter. As you point out, the former is awkward to call since it requires a with. The latter is idiomatic in shape, but functions which use a scope to launch a coroutine are typically named with the
In
suffix. Eg flow’s
produceIn
,
launchIn
d

df

11/30/2020, 5:29 PM
Thanks. That already helped 😉
5 Views