What is the correct way to define a method that re...
# coroutines
d
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
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
Thanks. That already helped 😉