-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking issue for bool::then_some
#64260
Comments
bool
to `Option<T>bool
to Option<T>
bool
to Option<T>
bool
to Option<T>
In trying this out, I was puzzled why the eager/lazy argument evaluation versions are |
There's a long discussion in the RFC about the naming. The current names are essentially placeholders. |
As a datapoint, this feature turns out pretty useful in proc macros, and I do think the current naming reads nicely: https://github.com/ruma/ruma-api/commit/cb00188e0371c544c4b8622d5a32731f2f6c54b3 |
@jplatte: that would be helpful to share on the RFC! |
It's been sometime since the PR implementation. |
@c410-f3r: the names were only decided upon quite recently. It was decided that we would wait a few versions before deciding to stabilise: rust-lang/rfcs#2757 (comment). |
Just came back to give this a test drive in a library I am working on. If we look at common current established patterns in
And looking at the current implementation of the
The inconsistency is pretty jarring/unexpected, to my mind. Specifically,
This is not just hypothetical; I am working on a library with a type that has a two-step fallible constructor. The intent is to be able to write something along the lines of: ...
impl Foo {
fn new() -> Option<Self> {
step_1_check().and_then(|| step_2_check().map(|| Self { /* ... */ }))
}
} but with the current plan developers would still have to resort to writing a custom combinator or pull in an external crate like Thoughts? |
You can call |
@U007D: Your comparison is between |
@U007D I completely agree. It was already tripping me up many times when using the IMO these functions should be named consistently with Or (if that's too much to include into std): Currently, according to the docs, |
How is the libs team feeling about this? Just ran into another case where I'd have liked to use this (on stable). |
@rustbot ping libs |
Error: Only Rust team members can ping teams. Please let |
cc @rust-lang/libs regarding naming and stabilisation: #64260 (comment). |
@varkor Did you mean to link some other comment? This one does not discuss naming. I honestly don’t remember the current status of this API.
Is this still the case or has some consensus been found since? |
@SimonSapin: sorry, I should have also linked to the comments above. I think the status is that, modulo outstanding concerns from some users about the naming, these methods are probably suitable for stabilisation.
The current names were suggested by @dtolnay. I'm personally happy with the existing names, but there have been some complaints: #64260 (comment), #64260 (comment). However, I think considering how much previous discussion we had on these names, without coming to a clear consensus, I think it's probably worth stabilising with the existing names, so these methods can be used. Perhaps starting an FCP would be the best way forward with these methods now: if any libs team members decide there are better alternatives, these could be raised during the FCP — otherwise, it would be good to be able to use these on stable. |
As a reminder, this issue currently tracks: impl bool {
pub fn then_some<T>(self, t: T) -> Option<T> {…}
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {…}
} I can live with this, and more time likely won’t bring new information or ideas at this point. @rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns: Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot resolve stabilize-only-then |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
I've opened a stabilisation PR for |
Stabilise `then` Stabilises the lazy variant of rust-lang#64260 now that the FCP [has ended](rust-lang#64260 (comment)). I've kept the original feature gate `bool_to_option` for the strict variant (`then_some`), and created a new insta-stable feature gate `lazy_bool_to_option` for `then`.
Stabilise `then` Stabilises the lazy variant of rust-lang#64260 now that the FCP [has ended](rust-lang#64260 (comment)). I've kept the original feature gate `bool_to_option` for the strict variant (`then_some`), and created a new insta-stable feature gate `lazy_bool_to_option` for `then`.
Stabilized; I think this tracking issue can be closed 🙂 |
Only |
It might be a good idea to move |
bool
to Option<T>
bool::then_some
I've updated the title and description. I don't think there's any need to create a new issue: that just causes unnecessary churn. |
Does that mean we're still considering stabilizing |
@slerpyyy: yes, see #64260 (comment). There was a desire to see how useful |
Closing this one in favour of #80967, so that we'll get a chance to issue a fresh FCP if we decide to in the future. |
As an experience report, I've recently found roughly the following code in the wild: (!is_uppercase)
.then(|| ())
.ok_or_else(|| ParseError(ParseErrorKind::Invalid, value.to_string())) It takes some mental puzzle-solving to figure out whether Don't think this is actionable in any way, but it's a useful real-world specimen to ponder. |
I think that is a good example that not everything is better with combinators :) I haven't decipered 100% it but I would write this as: if !is_uppercase {
return Err(ParseError(...));
} Personally, I've been using |
Agree, I've seen some similar code in the wild. I think that usually |
Tracking issue for the
bool::then_some
method, which abstract the following pattern:bool:then
has previously been stabilised as part of this feature gate: #79299RFC: rust-lang/rfcs#2757
Implementation PR: #64255
The text was updated successfully, but these errors were encountered: