Fail-fast when trying to consume a channel multiple times
See original GitHub issueChannels are designed to be used in fan-out fashion or in a pipeline fashion. Pipeline is conveniently performed with various operators like filter
, map
, etc (see PR #88) which are modeled after a Sequence
. However, a sequence is a multi-shot abstraction, for the following is perfectly file:
val s: Sequence<T> = ...
s.filter { ... }.forEach { it.doSomething() }
s.map { ... }.forEach { it.doSomethingElse() }
However, a similar code will not work in the same way with a ReceiveChannel
. ReceiveChannel
is more like Iterator
, than it is like a Sequence
. Each element of the ReceiveChannel
can be consumed only once. It is expected that this might be a common source of errors and misunderstanding, so the proposal is to make all the standard operators “fail-fast” when the channel is already being consumed by another operator. So, in the above example, if the types of s
was to be replaced with ReceiveChannel<T>
, then the invocation of s.map
(which comes after s.filter
) shall fail immediately.
P.S. It is a separate issue whether a truly Sequence
-like (Rx-like) higher-order abstraction shall be introduced for channels.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:13 (10 by maintainers)
Top GitHub Comments
I’m confused by the description, will consumeEach only be able to be called once? If so how would you do a fan-out?
just want to confirm that the following would still work: