-
Notifications
You must be signed in to change notification settings - Fork 308
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
at_most_one
should have a different return type
#707
Comments
at_most_one()
should have a different return typeat_most_one
should have a different return type
Hi there, thanks for the feedback. I admit that sometimes having a custom result type is better, but I found that sticking to Rust's That said, I'd appreciate if you could show what's exactly problematic with
And I think this the above is not your proposal:
I hope I don't miss the forest for the trees, but I think the zero/one/more is well-represented by our current API, in particular if the function's name is Please let me know if my view has any flaws. |
After taking some time to use the current As you said, using Semantically, since the function's name is Thank you for taking the time to consider my suggestion. |
at_most_one()
validates that an iterator has no more than one element, then returns it if the iterator had exactly one element, or some result that indicates that the iterator was empty. If it had more than one element, the original iterator is "restored" and returned back to the user. This is a useful concept that I occasionally need.That said, I believe the API of the
itertools
implementation is not as ergonomic as it could be. The existing implementation (#523) is based onItertools::exactly_one()
and returns aResult<Option<Self::Item>, ExactlyOneError<Self>>
, whereExactlyOneError
yields the same elements as the original iterator and also implementsError
.This return type has 2 flaws that keep me from using this function:
Ok()
) is not necessarily that the iterator had one element or zero elements. I often want to check that the iterator had exactly one element (and use it), but also behave differently in the zero elements case and in the many elements case (so in that case, zero elements is also "bad"). Similarly, having more than one element is not necessarily the only error case, it's just a different case. In other words, the zero/one/more than one distinction is better represented with three states and not two.Result<Option>
, which is somewhat annoying to use.I purpose changing the return type to something like
where
MoreThanOne
yields the same elements as the original iterator.I have implemented this feature (+ tests, docs) and will open a PR. #702 talks about making breaking changes, so this may be an appropriate time for this.
The text was updated successfully, but these errors were encountered: