-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow use of pipe operator in patterns. #1882
Conversation
|
I think you also have to introduce pattern grouping with parens |
As an example this currently works: match a {
f @ 'b' | f @ 'c' => foo(f),
_ => (),
} and this currently fails because of match a {
f @ 'b' | 'c' => foo(f),
_ => (),
} but if match a {
f @ ('b' | 'c') => foo(f),
_ => (),
} to bind |
text/0000-pipe-in-sub-expressions.md
Outdated
|
||
## Summary | ||
this RFC proposes allowing the `|` operator to be used within patterns in match | ||
statements, to allow for pattern matching with less boilerplate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should apply to all patterns, not just patterns in match
statements.
text/0000-pipe-in-sub-expressions.md
Outdated
} | ||
``` | ||
|
||
The solution to this would be to allow for `|` to be used within tuples. This |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be restricted to tuple patterns.
I think a better way to modify the grammar might look something like this: Remove the nonblock_match_clause
: maybe_outer_attrs pat maybe_guard FAT_ARROW nonblock_expr
| maybe_outer_attrs pat maybe_guard FAT_ARROW full_block_expr;
block_match_clause
: maybe_outer_attrs pat maybe_guard FAT_ARROW block; Change the top-level pat
: single_pat
| pat '|' single_pat; And add a new single_pat
: UNDERSCORE
| '&' pat
| '&' MUT pat
// ...
| '(' pat '|' single_pat ')'
; (There might be some ambiguity to resolve between that last rule and tuple patterns.) |
I am in favor of this, but this was also proposed last year. Have the reasons that was postponed changed/been addressed? (Note: WRT real world impact, I don't have links off-hand but I have run into wanting this 3-4 times in Diesel in the past 3 months or so. I'm sure I can dig up where I wanted this if needed) |
Well, for one, the MIR is done now. The Haskell community is discussing this right now, btw: ghc-proposals/ghc-proposals#43 (comment). http://gallium.inria.fr/%7Escherer/research/ambiguous_pattern_variables/ml_workshop_2016.abstract.pdf was especially useful and cited in that thread. |
@petrochenkov @Nemo157 Why would the @sgrif Well with the Rust roadmap, and I think this would fit well into lowering the learning curve for Rust, as it provides a much more intuitive syntax for pattern matching. |
@Aaronepower
is currently a valid match arm and it's grouped as I've just grepped rustc and found a few examples in real code:
In general, it would be natural for However, if |
This RFC is missing its motivation section, which is probably the most important section of an RFC! While this feature seems potentially nice, there is often a lot of unanticipated detail work necessary to push changes like this through (e.g, @petrochenkov is already turning up some interesting issues). Before we spend time hashing out all those details, there needs to be a clear case for why this is important to prioritize right now. |
One corner case in treating this as pure syntactic sugar: what if I write this: match a_big_tuple {
(0|1, 0|1, 0|1, 0|1, 0|1, 0|1, ...) => ... (Or anything else where you embed multiple alternations inside a pattern.) The obvious syntactic-sugar expansion would turn that into 2^n patterns. (The compiler ought to be able to optimize that in cases like this, but it could result in a large intermediate state if not handled specially.) Does this seem like a problem? |
Small data point: i am making a small tetris clone using the sdl2-crate. In my code i have a match expression which looks like this, if i understand it right: for event in event_pump.poll_iter() {
use sdl2::event::Event;
use sdl2::keyboard::Keycode::{Escape, Q};
match event {
Event::KeyDown {
keycode: Some(Escape),
..
}
| Event::KeyDown {
keycode: Some(Q),
..
} => break 'game,
_ => {},
} With this RFC i would be able to express more concisely like this: for event in event_pump.poll_iter() {
use sdl2::event::Event;
use sdl2::keyboard::Keycode::{Escape, Q};
match event {
Event::KeyDown {
keycode: Some(Escape | Q),
..
} => break 'game,
_ => {},
} In my opinion, this is more clear and thus less error-prone. |
Team member @aturon has proposed to close this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, 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. |
@Aaronepower To further clarify: if you're interested in seeing this go into Rust, that's still possible; it just needs further expansion into a full RFC that addresses all the issues raised in this thread. I'm personally interested in seeing this. |
I'm confused. What's the type of expression |
@liigo It's not an expression, it's a pattern for "try pattern |
Agree we should close. Although I do think it would be a good feature to have, I don't think it should be high priority right now. |
(Checking off for @pnkfelix, who is away) |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
According to the refference:
the pattern |
@nrc I disagree - it helps ergonomics a lot and should fit in well with the 2017 ergonomics initiative. |
I agree it helps ergonomics, but a little. |
Closing, as per FCP. Thanks @Aaronepower for the RFC! |
New RFC for this feature: #2535. |
rendered