-
Notifications
You must be signed in to change notification settings - Fork 19
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
Option::is_none_or
#212
Comments
Note that this proposed |
This was discussed in the libs meetup today. We generally have a relatively high bar for methods on types like Option and Result, and we don't think this passes that bar considering the alternatives available. |
Note that this only works if
|
With |
I had the opposite experience. Once I started using |
If we were to have a |
As an example of that: I quickly looked at rust-lang/rust#111873 for use cases, and the very first file in that change is: let is_macro_callsite = self
.session
.source_map()
.span_to_snippet(span)
- .map(|snippet| snippet.starts_with("#["))
- .unwrap_or(true);
+ .map_or(true, |snippet| snippet.starts_with("#[")); Which seems like an argument for
Which makes me think that it'd actually be easier to follow to use |
The naming is a problem though, For So, my opinions:
|
@m-ou-se yeah, this is a bad example, I agree 😅 Ig a better example would be something like pub fn is_ungated(&self, feature: Symbol) -> bool {
- self.spans.borrow().get(&feature).map_or(true, |spans| spans.is_empty())
+ self.spans.borrow().get(&feature).is_none_or(|spans| spans.is_empty())
} As using
Similarly I think |
Imo the Option/Result/Iterator/Stream/TryStream methods are a clusterfuck. In some cases it's In the current situation where each gets a bespoke name I end up not remembering the names and having to look them up quite often or relying on RA to offer the right thing. So my general stance is to not add any more unless there's a significantly stronger argument than consistency. |
@the8472 my main argument in this case is readability, all other options seem significantly less readable than |
That example also has a negation of the condition. It's hidden in the name: ungated. ;) I think this would be easier to read: pub fn is_gated(&self, feature: Symbol) -> bool {
self.spans.borrow().get(&feature).is_some_and(|spans| !spans.is_empty())
} "It's gated if there is a non-empty entry for the feature." (is_some_and(!)) versus "It's ungated if there is no entry for the feature or if the entry is empty." (is_none_or) or "It's ungated if there is no non-empty entry for the feature." (!is_some_and(!)) |
Rrright. I'll check/fix the PR more closely and see if there are still valid use cases. |
What is the bar for reopening this ACP? Here are some recent examples people posted where
Generally this is not surprising; |
I recognize the importance of maintaining high standards for methods on Option and Result. However, I also believe Would it be possible to reconsider this proposal? |
Another usecase in the compiler: tys.last().iter().all(|ty| is_very_trivially_sized(**ty)) This is iterating over an tys.last().is_none_or(|ty| is_very_trivially_sized(**ty)) I can of course do !tys.last().is_some_and(|ty| !is_very_trivially_sized(**ty)) but that is extremely hard to reason about. Having
I think that requires someone to file a new ACP. Do you want to do that? |
I've also found this to be quite useful in my own code, and found that |
We discussed this in the libs-api meeting again today and we're happy to accept it. This is primarily based on the experience reports which show that only relying on With that said, we would also like people to provide feedback on rust-lang/rfcs#3573 which is a language feature which provides similar functionality. |
Add Option::is_none_or ACP: rust-lang/libs-team#212
Add Option::is_none_or ACP: rust-lang/libs-team#212
Add Option::is_none_or ACP: rust-lang/libs-team#212
Rollup merge of rust-lang#126328 - RalfJung:is_none_or, r=workingjubilee Add Option::is_none_or ACP: rust-lang/libs-team#212
Add Option::is_none_or ACP: rust-lang/libs-team#212
Add Option::is_none_or ACP: rust-lang/libs-team#212
Proposal
Problem statement
It seems like there is a common use-case to check if an option is
None
or some condition holds for its value.Motivation, use-cases
Similarly to how we have
Option::is_some_and
which is basically.map_or(false, ...)
, sometimes it is desirable to have the opposite "default" value, i.e. sometimes a better name for.map_or(true, ...)
is wanted. See for example comments on theOption::is_some_and
tracking issue:See also 45 occurrences of
.map_or(true, ...)
in the rustc itself:list
Solution sketches
Links and related work
There is an open PR implementing this, that was untouched for 8 moths: rust-lang/rust#100602
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: