Skip to content
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

Should the wide arrow in match-case be dropped? #5119

Open
CJ-Johnson opened this issue Mar 12, 2025 · 5 comments
Open

Should the wide arrow in match-case be dropped? #5119

CJ-Johnson opened this issue Mar 12, 2025 · 5 comments
Labels
leads question A question for the leads team

Comments

@CJ-Johnson
Copy link
Contributor

CJ-Johnson commented Mar 12, 2025

Summary of issue:

New Information: The syntax for match-case was chosen before the syntax of lambdas was designed. Since then, lambdas landed in the language in #3848. This means the syntax of lambdas could potentially justify reconsidering the syntax of match-case.

Issue: The arrow in a case of match-case uses the => syntax followed by a curly braced body. This is incongruent to the syntax of all function kinds, with the lambdas proposal landed. The "wide arrow" means a function returns auto deduced from a single-expression body followed by a semicolon.

Leads Question: Would it be ok to drop the wide arrow entirely from the match-case syntax? Assuming the parens are mandatory regular parens, as suggested in #5118, there shouldn't be any syntactic ambiguities if match-case was written like the following:

match (F()) {
  // Notice the lack of wide arrow
  case (x: auto) {
    // ...
  }
}

Details:

No response

Any other information that you want to share?

No response

@CJ-Johnson CJ-Johnson added the leads question A question for the leads team label Mar 12, 2025
@CJ-Johnson CJ-Johnson changed the title Should the fat arrow in match-case be dropped? Should the wide arrow in match-case be dropped? Mar 13, 2025
@zygoloid
Copy link
Contributor

zygoloid commented Mar 26, 2025

The argument presented here seems compelling to me. In other contexts, we use => to introduce an expression, and just use braces with no other marking to introduce the body of a function or control flow statement.

At some point in the future, it might be reasonable to use => in an expression form of match:

let n: i32 = match (F()) { case 0 => 5, case _: i32 => 10 };

... but using it to introduce a block of code seems inconsistent.

If the parens in case were mandatory, the argument here would be stronger, but I think it's syntactically acceptable to have a pattern followed by a braced block; this seems analogous to what happens in a function declaration, where we have a return type expression followed by a braced block:

fn F() -> something {
case something {

This would mean that we can't we support a non-expression pattern syntax of the form some_pattern { ... (unless we're prepared to use whitespace to distinguish the meaning of the {, which seems likely to be a bit too fragile in this case).

@CJ-Johnson
Copy link
Contributor Author

Thanks for the reply! Glad you found it compelling :)

I was under the impression that we were intending the parens in case to be mandatory. Especially in the face of so-called "guards" that can come after the case.

match (F()) {
  case (x: auto) if (x % 2) {
    return "It was odd";
  }
}

@geoffromer Can you speak to the use of guards and how that interacts with the wide arrow and case-mandated parens?

@CJ-Johnson
Copy link
Contributor Author

Ah, I see now there is code that does not have parens in the case, as in

. My mistake. Perhaps #5118 should be amended to ask for them to become mandatory?

@CJ-Johnson
Copy link
Contributor Author

CJ-Johnson commented Mar 27, 2025

I worry that if parens are not mandatory, the lack of wide arrow will be ambiguous w/r/t struct literals versus the body of the case.

// Without mandatory parens...
match (F()) {
  // Ambiguous?
  case {} {  return 10; }
}
// With mandatory parens...
match (F()) {
  // Unambiguous?
  case ({}) {  return 10; }
}

@zygoloid
Copy link
Contributor

I worry that if parens are not mandatory, the lack of wide arrow will be ambiguous w/r/t struct literals versus the body of the case.

I think that's exactly analogous to

fn F() -> {} { return 10; }

... which is parseable (at least by machines; humans are another question), because we don't have any expression suffixes starting with {.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
leads question A question for the leads team
Projects
None yet
Development

No branches or pull requests

2 participants