-
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: Add a matches!(expression, pattern) macro. #163
Conversation
This was first proposed as rust-lang/rust#14685 and discussed by the team in the 2014-07-08 weekly meeting. The main concern there as far as I can tell was about adding things to the prelude. See the Alternatives part of this RFC for more discussion on this. |
|
||
# Unresolved questions | ||
|
||
Should this be a language feature rather than a macro? If so, what would the syntax look like? |
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 is the is
operator that was discussed at the if let
proposal.
The most reasonable version of it IMHO is that is
is a keyword, and is an infix binary operator, where EXPRESSION is PATTERN
is an expression of type bool
and does basically the same thing as matches!(EXPRESSION, PATTERN)
, and doesn't allow binding names.
(This is intended as an answer to the second question, without implying any opinion about the first.)
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.
It's worth noting that the benefit of matches!()
over is
is that matches!()
can very easily allow for alternative patterns, as is being suggested here. However is
cannot support alternative patterns without either being confusing (because |
is a valid operator that one would expect to operate on the bool
result of the is
operator) or requiring additional syntax to enclose the set of patterns.
While I appreciate the syntactic simplicity of is
, this suggests that matches!()
may be the better approach (what with being implementable as a macro_rules!
macro and not requiring the language grammar be extended in any way).
+1 looks great! This is one of these small things which can make your life a lot nicer. |
👍 I agree that this is complementary to |
This is both very useful and complementary to |
NB. this is a |
This would be a very welcome addition. One comment: until I saw the macro itself, I was expecting RFC author proposes |
@huonw What’s a Cargo plugin repository? It sounds related to the second alternative in the RFC. @esbullington Yes, |
@SimonSapin something like https://github.com/BurntSushi/quickcheck/blob/master/quickcheck_macros/Cargo.toml , loaded like https://github.com/BurntSushi/quickcheck/blob/master/examples/attribute.rs and still usable as a normal dependency https://github.com/BurntSushi/quickcheck#installation |
I really like this proposal, and prefer the |
One consideration with respect to the question of whether a Having Would we also be ready to remove them in favor of telling people to use |
if some_expression.is_some() { ... } versus if some_expression is Some(_) { ... } versus if matches!(some_expression, Some(_)) { ... } Personally I think that the last variant is totally ugly. There is ambiguity problem with enum X {
A { x: int }
}
if some_expression is A { .. } { ... } |
@netvl I assume the same 'fix' could be applied as in some other points in the grammar with similar potential ambiguities, where struct literals (edit: or structural enum variants I guess) are disallowed except if wrapped in parentheses (IIRC). Perhaps someone more familiar with this could clarify. |
@glaebhoerl, yes, that would be quite natural. Just wanted to point this out. |
I don't particularly like As for macro vs Regarding |
How about |
@liigo The latter has no benefits over |
@kballard I'm not deeply familiar with the grammer, but couldn't you just allow enclosing the RHS of |
@glaebhoerl You could certainly create a rule like that, but it just looks weird to me. Maybe it's not that bad, I don't know, but it's hard to argue that it's good when we could get the same functionality through a macro. |
Basically, while I like the simplicity of |
Yes, that was what I was thinking at first as well; while the possibility that |
Sounds good to me. I prefer Visually this looks good to me:
but this might be confused with bit-wise or. If that's not possible I prefer the macro approach over |
I think this is very useful. +1 |
This was discussed in today's weekly meeting, and the conclusion is that we would like to not merge this for now. We are primarily worried about adding such a feature to the prelude. We all very much desire a macro import/export system, and this RFC would likely be a shoo-in if we were to have one (so long as it were not in the prelude). With that in mind, the decision was made to postpone this for now until such a system exists. Thanks for the RFC, however, @SimonSapin! |
Visually, using an I saw that the |
I’ve published this macro on crates.io: https://crates.io/crates/matches |
Nightly broke this, and it’s apparently not possible to fix without changing the usage syntax :( rust-lang/rust#20563 (comment) |
Since you can now |
The discussion in rust-lang/rust#35896 appears to be ongoing and not quite settled yet. Also, this proposal is very old and I have limited bandwidth to champion it these days. Consider making a new RFC / PR. (Feel free to copy some or all of this one.) |
Rendered view: https://github.com/SimonSapin/rfcs/blob/matches-macro/active/0000-matches-macro.md
Please note: This is different from the
if let
proposal. I believe the two proposals are complementary rather than competing.