-
Notifications
You must be signed in to change notification settings - Fork 3
Should we have different syntax and semantics for statement and expression match
? (or if let
?)
#114
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
Comments
It's probably worth reading rust-lang/rfcs#2260 before supporting Or the merged version of the RFC: (not the same PR) rust-lang/rfcs#2497 (Note: I just realized we already support There are some objections in rust-lang/rfcs#2260 (comment), and an alternative in the next comment: rust-lang/rfcs#2260 (comment). The alternative is basically Haskell's multi-way Also mentioned in the links above, apparently C# supports pattern matching in The main difference between If we implement multi-way With multiple branches:
With just one branch, we should be able to avoid the extra indentation:
Or we could call multi-branch version If we go with the Or use In https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns they show examples with patterns I don't understand why they used If we use Also, is C# even real? They have relational patterns: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#relational-patterns which allows things like |
Here's a plan:
In the Rust RFC people decided against this more general form, but I can't find what the problem is. It's certainly not a simple feature, but I think it may worth it. |
23b667d replaces single branch |
With
In terms of expressivity, they're the same. (Note: we haven't implemented guards in I'm still not 100% sure about the multi-branch
However this can also be considered a good thing, as you no longer have a reason to prefer Because "if: " is exactly one indentation level long, you can also do this:
I'm not sure if this looks better or worse. Note that we still need an |
Right now we have one
match
syntax which can be used as an expression or statement.When used as a statement not in return position, we allow the branches to have different types. See these tests for examples: https://github.com/fir-lang/fir/blob/eb2236a27a1730af97e7f17b0ddb1f1114c44fa3/tests/IfMatchStmtChecking.fir
In both exprs and stmts though we check exhaustiveness, and in runtime, if the expr or stmt doesn't match the scrutinee the interpreter panics.
I think in statement for we could allow not being exhaustive, i.e. add an implicit
_: ()
at the end of every non-exhaustivematch
statement.However a concern is that we'll have the same syntax for two things that work quite differently. So maybe a new keyword would be useful for the statement form, maybe
switch
.Syntax-wise everything else other than the keyword should be the same.
The use case is similar to
if let Some(x) = ... { ... }
in Rust, in code likefir/compiler/AstPrinter.fir
Lines 109 to 115 in cd69304
Here ideally I want to be able to omit the
Option.None
case. Withswitch
it would look like this:When we have just one branch Rust style
if let
can be more concise:And if we add
if let
, we can add it in full generality (allow in conjunctions) which would make statementswitch
mostly redundant.So maybe we just want
if let
?The text was updated successfully, but these errors were encountered: