Closed
Description
From the docs:
Logical-or patterns are useful for having multiple cases share a body in switch expressions or statements:
var isPrimary = switch (color) { Color.red || Color.yellow || Color.blue => true, _ => false };
When we have a switch statement like the following, the assist for turning it into a switch expression doesn't show:
int a(int x) {
switch (x) {
case 0:
return 0;
case 2:
case 1:
return 1;
default:
return throw UnimplementedError();
}
}
Only if there is a single case with a specific body:
In this case, I believe the assist should appear and create a logical-or pattern for them.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
use_switch_expressions
#58862FMorschel commentedon May 29, 2024
I'm not sure if you agree, but I think maybe there could be an assist for merging all of those cases with a logical-or pattern. And that could be run separately or with this assist.
So cases like the above could become:
And then the assist for replacing the switch does appear.
FMorschel commentedon Jul 8, 2024
Created #56180 for the above comment.
FMorschel commentedon Aug 6, 2024
Looking at the code and blame for this feature and trying to understand it to implement it by myself, I just wanted to point out that this is already something working but for some reason not all cases.
Here is the original issue: #54567
If I try to do something like:
Then the assist does appear. So something is not working as it should. Maybe something related to integers not having a finite amount of options?
Will take a look into it whenever I can, just a little update here.
bwilkerson commentedon Aug 6, 2024
In the latest version (on HEAD), if I paste the original example into a file:
When I click on the
switch
keyword and open the lightbulb menu, I see a 'Convert to switch expression' option that produces the following:It appears to me that the limitation has been fixed, but the issue wasn't updated appropriately.
If I'm missing something please let me know and I'll re-open the issue, but for now I'm going to close it.
||
#52958