-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fail-fast when trying to consume a channel multiple times #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
just want to confirm that the following would still work:
|
I'm confused by the description, will consumeEach only be able to be called once? If so how would you do a fan-out? |
@bdavisx Thank you for observation. This indeed can be confusing and we should think on how to make it less confusing. They idea is that for fan-out you don't consume the channel. Instead, you simply invoke |
Thanks and yes, very confusing because the current guide/examples fan out section doesn't use |
I changed the doc to use an |
Will a |
@jcornaz probably no, because it will be an additional source of confusion. Usually you need |
Isn't the confusion already here? I don't think it is particularity clearer that However I agree that user may see the |
The difference between "consuming" and "iterating in fan-out fashion" is indeed confusing and more design effort is needed to make it more explicit. |
In regular collections we're all used to forEach being terminal and onEach not, why have consume in the first place and not reuse familiar concepts (apart from the suspending nature of consume which is anyways in the function's signature...)? Or maybe there's another aspect here that I didn't get? |
Initially
But this works:
We can revert this decision, rename it back to |
How about a |
Status update: won't be fixed. There are valid use-cases with a fan-out style (thus concurrent |
Channels 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 aSequence
. However, a sequence is a multi-shot abstraction, for the following is perfectly file:However, a similar code will not work in the same way with a
ReceiveChannel
.ReceiveChannel
is more likeIterator
, than it is like aSequence
. Each element of theReceiveChannel
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 ofs
was to be replaced withReceiveChannel<T>
, then the invocation ofs.map
(which comes afters.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.The text was updated successfully, but these errors were encountered: