Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Add a matches!(expression, pattern) macro. #163
Conversation
This comment has been minimized.
This comment has been minimized.
|
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. |
glaebhoerl
reviewed
Jul 14, 2014
|
|
||
| # Unresolved questions | ||
|
|
||
| Should this be a language feature rather than a macro? If so, what would the syntax look like? |
This comment has been minimized.
This comment has been minimized.
glaebhoerl
Jul 14, 2014
Contributor
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.)
This comment has been minimized.
This comment has been minimized.
lilyball
Jul 15, 2014
Contributor
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).
This comment has been minimized.
This comment has been minimized.
netvl
commented on active/0000-matches-macro.md in 4a906d7
Jul 14, 2014
|
Looks like an extra line. |
This comment has been minimized.
This comment has been minimized.
|
Indeed, thanks. Fixed. |
This comment has been minimized.
This comment has been minimized.
netvl
commented
Jul 14, 2014
|
+1 looks great! This is one of these small things which can make your life a lot nicer. |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
Valloric
commented
Jul 15, 2014
|
This is both very useful and complementary to |
This comment has been minimized.
This comment has been minimized.
|
NB. this is a |
This comment has been minimized.
This comment has been minimized.
esbullington
commented
Jul 15, 2014
|
This would be a very welcome addition. One comment: until I saw the macro itself, I was expecting RFC author proposes |
This comment has been minimized.
This comment has been minimized.
|
@huonw What’s a Cargo plugin repository? It sounds related to the second alternative in the RFC. @esbullington Yes, |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
asb
commented
Jul 16, 2014
|
I really like this proposal, and prefer the |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
netvl
commented
Jul 16, 2014
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 { .. } { ... } |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
netvl
commented
Jul 16, 2014
|
@glaebhoerl, yes, that would be quite natural. Just wanted to point this out. |
This comment has been minimized.
This comment has been minimized.
|
I don't particularly like As for macro vs Regarding |
This comment has been minimized.
This comment has been minimized.
|
How about |
This comment has been minimized.
This comment has been minimized.
|
@liigo The latter has no benefits over |
This comment has been minimized.
This comment has been minimized.
|
@kballard I'm not deeply familiar with the grammer, but couldn't you just allow enclosing the RHS of |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
Basically, while I like the simplicity of |
This comment has been minimized.
This comment has been minimized.
|
Yes, that was what I was thinking at first as well; while the possibility that |
This comment has been minimized.
This comment has been minimized.
treeman
commented
Jul 17, 2014
|
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 |
This comment has been minimized.
This comment has been minimized.
|
I think this is very useful. +1 |
This comment has been minimized.
This comment has been minimized.
|
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! |
alexcrichton
closed this
Sep 3, 2014
alexcrichton
added
postponed
and removed
postponed
labels
Sep 3, 2014
alexcrichton
added
the
postponed
label
Sep 3, 2014
SimonSapin
referenced this pull request
Sep 19, 2014
Closed
Add a shorter macro for println!("{:?}", foo) #12015
rust-highfive
referenced this pull request
Sep 24, 2014
Open
RFC: Add a matches!(expression, pattern) macro. #264
This comment has been minimized.
This comment has been minimized.
|
Visually, using an I saw that the |
This comment has been minimized.
This comment has been minimized.
|
I’ve published this macro on crates.io: https://crates.io/crates/matches |
This comment has been minimized.
This comment has been minimized.
|
Nightly broke this, and it’s apparently not possible to fix without changing the usage syntax :( rust-lang/rust#20563 (comment) |
SimonSapin
referenced this pull request
Sep 27, 2017
Closed
Macroless mapping of patterns -> bool #2153
This comment has been minimized.
This comment has been minimized.
Laaas
commented
Jun 9, 2018
|
Since you can now |
SimonSapin
referenced this pull request
Jun 9, 2018
Closed
Add unwrap_match! macro which is a general unwrap for enums #13
This comment has been minimized.
This comment has been minimized.
|
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.) |
SimonSapin commentedJul 14, 2014
Rendered view: https://github.com/SimonSapin/rfcs/blob/matches-macro/active/0000-matches-macro.md
Please note: This is different from the
if letproposal. I believe the two proposals are complementary rather than competing.