-
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 match/in statements #2144
Changes from 1 commit
d61d1f2
73e34ec
daf503d
8f064a9
dee68cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,12 +31,12 @@ checked for exhaustiveness, be executed if more than one of them match the value | |
|
||
Basic Syntax: | ||
```rust | ||
match val in { | ||
match fallthrough val { | ||
pat | pat => expr, | ||
pat => expr | ||
} | ||
|
||
match val in { | ||
match fallthrough val { | ||
pat | pat => expr, | ||
pat => expr | ||
} else { | ||
|
@@ -45,23 +45,24 @@ match val in { | |
``` | ||
|
||
Benefits of this syntax: | ||
1. No new keywords need to be used. This is good thing since it means for a relatively small | ||
addition there would be no code breaks of existing code with this change. | ||
2. The `in` seems to imply that it sort of like an "iterator" of statements and will go through | ||
each of them in turn. | ||
1. Even though a new keyword has been made it will not break any code because Rust is a context | ||
sensitive language. And adding such a keyword increases the perceptual area of the new syntax | ||
so as to make it clear which type of match is being used. | ||
2. The word `fallthrough` is used because it implies that after a branch is finished then the | ||
control falls through to the check of the next branch. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is fair and I am pretty sure that adding an additional keyword here wouldn't break any code since if a variable was called what ever it was that would be able to be checked but that is definitely something to consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, adding the additional "keyword" should work w/o backwards incompatibility since there's no (iirc) parsing rule "<expr> <expr>". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a correct DFA adding that state would be rather complicated but I agree is do able There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... DFA for what? Lexing? The language is not regular... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not actually looked that deep into Rust I didn't consider it to be that high order, though tbh I don't know why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rust's lexical grammar is already context-sensitive due to Not a single real programming language is regular (not counting assembly and machine code). Fun fact: Even the simple-looking Lua grammar requires infinite lookahead to parse and is thus harder to parse than Rust. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More details here: https://stackoverflow.com/a/43693150/1063961 and here https://stackoverflow.com/a/43693194/1063961 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Instead I'd like to add |
||
Meaning of parts: | ||
1. The `else` is used in a similar sort of vein to that of the `_` pattern in normal matches. | ||
The expression enclosed within this is only executed if none of the patterns within the | ||
`match/in` expression are matched. If `else` and `_` are both present then the code within the | ||
`match/fallthrough` expression are matched. If `else` and `_` are both present then the code within the | ||
`else` would be marked as unreadable. | ||
|
||
Edge cases: | ||
1. If the `_` pattern in present in any of the contained matches and the `else` block is also | ||
present then a `unreachable_code` lint is emitted on the code within the `else` block | ||
2. Since the main reason for using a `match` is the exhaustiveness checks as long as there isn't | ||
an `else` block then the compiler will output an error for `non-exhaustive patterns` if not all | ||
branches of the `match/in` are exhaustive. | ||
branches of the `match/fallthrough` are exhaustive. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another possibility would be to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So would it be the following (for clarification): match many x {
_ if x % 5 == 0 => expr,
! => expr
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely see the appeal of it, since as you say it is much more terse, but I think that it would be more confusing since it is in the area of pat/expr of the expression but does not follow the same "rules" as the other patterns since it will only be executed if no other patterns are executed sort of how the |
||
Implementation Assumptions: | ||
1. Assuming that a `match` expression is currently implemented similar to a long chain of | ||
|
@@ -78,9 +79,9 @@ branches then it is set to immediately after the `else` block. | |
# How We Teach This | ||
[how-we-teach-this]: #how-we-teach-this | ||
|
||
This should be called `match/in` expressions since that is the combination of keywords that are | ||
used similar to `for/in` expressions. This idea would be best presented as a continuation of | ||
existing Rust patterns since it expands on the `match` expression. | ||
This should be called `match/fallthrough` expressions since that is the combination of keywords | ||
that are used. This idea would be best presented as a continuation of existing Rust patterns | ||
since it expands on the `match` expression. | ||
|
||
This proposal should be introduced to new users right after `match` expressions are taught. This | ||
is the best time to teach it since it appears as an extension of that syntax and the ideas that | ||
|
@@ -116,7 +117,7 @@ match cmp.compare(&array[left], &array[right]) { | |
``` | ||
into | ||
```rust | ||
match cmp.compare(&array[left], &array[right]) in { | ||
match fallthrough cmp.compare(&array[left], &array[right]) { | ||
Less | Equal => { | ||
merged.push(array[left]); | ||
left += 1; | ||
|
@@ -148,7 +149,7 @@ for x in 1...100 { | |
into | ||
```rust | ||
for x in 1...100 { | ||
match x in { | ||
match fallthrough x { | ||
_ if x % 5 == 0 => print!("fizz"), | ||
_ if x % 7 == 0 => print!("buzz") | ||
} else { | ||
|
@@ -180,4 +181,4 @@ to have and so not implementing it could be an option. | |
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
Whether or not `match/in` makes sense for this sort of control flow. | ||
Whether or not `match/fallthrough` makes sense for this sort of control flow. |
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 deviation from the
_ => ...
syntax used by "regular" match is a bit unfortunateThere 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 may be a bit unfortunate but if you consider the meaning of
_
it makes sense. The_
pattern means to match everything.