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

zak.taccardi

05/08/2019, 7:22 PM
What’s the reasoning that
Channel<T>
and
Flow<T>
don’t share the same backing interface? It seems unfortunate that the two won’t be able to share the same operators
e

elizarov

05/08/2019, 7:26 PM
What do you think they have in common and what operations this interface shall support?
z

zak.taccardi

05/08/2019, 7:28 PM
You originally stated that one of the goals was that they would both share the same interface so an operator for one would work for the other. I would imagine that you guys encountered a good reason as to why you decided to reverse course on it - was just curious what that reason is
The common theme is both
Channel<T>
and
Flow<T>
are that they both emit values of
T
. The implementation detail is how they emit values
a

altavir

05/08/2019, 7:37 PM
Flow does not emit anything, it accepts consumer.
z

zak.taccardi

05/08/2019, 7:43 PM
Sure, but as a user of the API, I see both
Flow<T>
and
Channel<T>
as a way to consume multiple values of
T
over time
l

louiscad

05/08/2019, 8:35 PM
You mean
Flow
and
ReceiveChannel
. They are still different as consuming or iterating a channel doesn't share the scope with the producer (and the produced may not even be in a coroutine at all), while a
Flow
collecting coroutine will share its scope with the emitting collector, passing any failures through. Also, a
Flow
emitting collector can always throttle the collecting coroutine, which is not the case for
Channel
.
👍 1
z

zak.taccardi

05/08/2019, 8:35 PM
oh yeah, my bad.
Flow<T>
and
ReceiveChannel<T>
, not
Channel<T>
e

elizarov

05/08/2019, 10:29 PM
We came to think they are too different to share an interface. Channels are single use, hot, and must be consumed, while flows are multi-use and cold. So you need to be explicit when converting them using
asFlow()
.
👍 1
l

louiscad

05/08/2019, 11:10 PM
@elizarov There's no
asFlow()
extension for
ReceiveChannel
as of kotlinx.coroutines 1.2.1
p

Paul Woitaschek

05/09/2019, 9:29 AM
There is
asFlow()
on
BroadcastChannel
e

elizarov

05/09/2019, 2:49 PM
asFlow() = flow { consumeEach { emit(it) } }
l

louiscad

05/09/2019, 4:09 PM
Sure, and crazy how simple it is, but is there a reason it is not in kotlinx.coroutines?
e

elizarov

05/09/2019, 4:48 PM
Name and contract. What happens if you do it multiple times? Shall it be named
consumeAsFlow
? Etc....
👍 1
7 Views