Skip to content

Should we have different syntax and semantics for statement and expression match? (or if let?) #114

@osa1

Description

@osa1

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-exhaustive match 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 like

fir/compiler/AstPrinter.fir

Lines 109 to 115 in cd69304

match extension:
Option.None: ()
Option.Some(extension):
if fields.len() == 0:
doc = Doc.str("..")
else:
doc += Doc.str(", ..")

Here ideally I want to be able to omit the Option.None case. With switch it would look like this:

switch extension:
    Option.Some(extension):
        if fields.len() == 0:
            doc = Doc.str("..")
        else:
            doc += Doc.str(", ..")

When we have just one branch Rust style if let can be more concise:

if let Option.Some(extension):
    if fields.len() == 0:
        doc = Doc.str("..")
    else:
        doc += Doc.str(", ..")

And if we add if let, we can add it in full generality (allow in conjunctions) which would make statement switch mostly redundant.

So maybe we just want if let?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions