Skip to content

Assist for merging single-body swicth cases with a logical-or pattern #56180

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

Open
FMorschel opened this issue Jul 8, 2024 · 7 comments
Open
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-assist Issues with analysis server assists P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@FMorschel
Copy link
Contributor

I'd like to ask for an assist for merging single-body swicth cases with a logical-or pattern

So cases like:

int a(int x) {
  switch (x) {
    case 0:
      return 0;
    case 2:
    case 1:
      return 1;
    default:
      return throw UnimplementedError();
  }
}

Can become:

switch (x) {
  case 0:
    return 0;
  case 1 || 2: // or swapped I'm not sure what to put first here (not sure it matters, just an UX question)
    return 1;
  default:
    return throw UnimplementedError();
}

Originally posted by @FMorschel in #55861 (comment)

@dart-github-bot
Copy link
Collaborator

Summary: The user proposes a syntax change to Dart's switch statement, allowing multiple case values to be combined using logical OR (||) for more concise code. They specifically want to merge cases with single-body statements.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Jul 8, 2024
@devoncarew devoncarew added legacy-area-analyzer Use area-devexp instead. and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jul 8, 2024
@bwilkerson bwilkerson added the devexp-assist Issues with analysis server assists label Jul 8, 2024
@keertip keertip added the P3 A lower priority bug or feature request label Jul 9, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Jul 19, 2024
@FMorschel
Copy link
Contributor Author

FMorschel commented Aug 7, 2024

Now that #55861 is closed, a workaround is to do both conversions to and from switch expression. It will give the same result asked above.

But it obviously just works when the switch statement can be converted.

@FMorschel
Copy link
Contributor Author

If #59529 is accepted it would work great with this as well.

@FMorschel
Copy link
Contributor Author

Also, #59530 (if accepted) would help ensure duplicates are removed as well as unreachable_switch_case.

@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
@FMorschel
Copy link
Contributor Author

This may be a good starting point @paudel-milan. Again, feel free to reach out to me if you have any doubts.

@bwilkerson
Copy link
Member

... or swapped I'm not sure what to put first here ...

I think the feature should preserve the original order. Switch cases are rarely integers, so in most cases there's no other good option, but more importantly, there's likely to be a reason for the existing order and it would be better to preserve any semantic information there might be.

@paudel-milan
Copy link

"Thank you, @FMorschel! I appreciate the guidance and the opportunity to contribute. I'll go through the provided resources and reach out if I have any questions. Looking forward to contributing!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-assist Issues with analysis server assists P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants