-
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
RFC: Alternatives in patterns #1500
Conversation
match new_type { | ||
NewType(Ok(e) | Err(e), num) => println!("ok with {}: {}", num, e) | ||
} | ||
``` |
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 only works if for Result<T, T> , when Ok and Err both has same value type. I don't think this is good use case.
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.
Well, that's an example, but for generalicity's sake can be replaced with Ok(_) | Err(_)
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.
No, it can't. Because e
is used in arm. If you don't want to use, then it may be written as NewType(_, num)
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.
Ah, right. But I've used Result not to introduce a new enum.
Can you elaborate on this? I expect the second form to only be allowed for irrefutable patterns, just like right now. Consequently, the first example should however be disallowed as the specified pattern would have to be irrefutable (or else one the |
efe9214
to
93fe27d
Compare
@main-- I agree with both points, though irrefutable patterns may be allowed for Updated a proposed RFC considering irrefutable patterns in |
@durka does this affect if let (A(e) | B(e)) = thing {}
let (A(e) | B(e) | C(e)) = thing; Edit: Can you elaborate: does, and if yes, why this affects non-macro code? |
@White-Oak The macro follow sets are meant to provide a compatibility guarantee for macros. Currently So the issue is that this will break macros, even if it doesn't affect non-macro code. |
@jonas-schievink I see. |
Nope, that's comparable to |
If we're careful we won't break macros, because we'll just require that top level of parentheses for it to be considered a single pattern. Alternatively we could extend the syntax of Without top-level parentheses, it would lead to ambiguities anyway, because arguments to closures are patterns; consider this code (where do the closure arguments end and the body begin?): enum Foo { A(i32), B(i32) }
let closure = | Foo::A(i) | Foo::B(i) | { i + 1 }; |
@durka is there a way to handle |
I don't see why not. But don't take my word for it.
|
fe4fc9c
to
33c96a3
Compare
Updated RFC adressing "requirement of surrounding parens" for multiple variants.
match {
NewType((Ok(e) | Err(e)), num) => ()
} or is it possible to omit parens: match {
NewType(Ok(e) | Err(e), num) => ()
} |
3996484
to
e5dd60f
Compare
@White-Oak Thanks for the RFC! The lang team discussed the RFC while attempting to find a shepherd for it. Our feeling is that, while we may eventually want to support such a syntax, there is not strong enough motivation at present to make it a priority, especially with so much else in flight in the compiler with the transition to MIR. As such, we're going to close as postponed. In a future incarnation of this RFC, we'd like to see more detailed motivation with real-world use-cases where the feature makes a significant impact. |
New RFC for this feature: #2535. |
Rendered view