-
Notifications
You must be signed in to change notification settings - Fork 209
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
Make case optional in switch statements? #2618
Comments
I just saw the issue you filed but take a look at my suggestion here: #2126 (comment). The short answer is, yes, think we could and should eliminate |
I personally disagree. I don't think we should not remove It's slightly more verbose, but it also means that I disagree strongly with making expression-switch and element-switch have different syntaxes. If we omit I'd prefer: // statement
switch (e) {
case p when e:
case p when e:
statements
default:
statements
}
// expression:
var _ = switch (e) {
case p when e =>
case p when e =>
expression
default =>
expression
}
// element:
var _ = [
switch (e) {
case p when e =>
case p when e =>
element
default =>
element
}
];
// if statement
if (e case p when e)
statement
else
statement
// if element
var _ = [if (e case p when e) element else element]; No separator characters needed between cases, the case body is terminated by the following If we do remove the I'll want the same thing for switch-element cases. They are closer to expressions than to statements, even if they can contain |
I don't see a super compelling reason to make switch expressions consistent with switch statements. That isn't how the corresponding expression forms for conditionals work. If you line them up:
As you can see, the element form always strictly follows the statement form. Then the expression form is basically its own thing. I like consistency in general, of course. But I think the context for statements and expressions are different enough that they push those syntaxes in different directions. For expressions, brevity is more important so that you don't lose sight of the larger surrounding structure. And we don't need There is an interesting question about what the body of switch elements should be. Since it follows switch statements, that suggests allowing a series of elements in each case: var items = [
switch (c) {
case 'three':
1,
2,
3,
case 'many':
4,
...[5, 6, 7],
case 'nested':
if (d)
8,
9,
for (var i = 10; i < 20; i++)
i
}
]; I'm not sure if that's a good idea or not, though. |
I love the idea of "multi-elements" in elements in general (the I don't want to do That said, it does look very compelling when written out this way. I think I might be able to live with it :) (Are there any other control flow constructs that we want to introduce in expressions? #2505 is just about break/continue/return, which are trivial. We have switch now. We have I think we should allow |
I don't love
I would have a lot more faith in our compilers optimizing
\o/
Yeah, I would definitely like foo(int n) {
String s;
switch (n) {
case 0: s = 'blah';
case 1: s = 'whatever';
default: return 'invalid';
}
return s.toUpperCase();
} It would be nice to convert that to a switch expression, but the case that returns makes that not work. But if return was an expression, you could do: foo(int n) {
return switch (n) {
0 => 'blah',
1 => 'whatever',
_ => return 'invalid';
}.toUpperCase();
}
I would like to have int n; // Gross. :(
try {
n = int.tryParse('1234');
} on FormatException {
n = 0;
} It would be cool if you could write: var n = try {
int.tryParse('1234');
} on FormatException {
0;
} But that gets into weird territory around making blocks expressions and so far I've been afraid to go there.
I can't recall which one, but I know there's some language out there that treats loops as expressions and allows a value after |
Closing this. I think we've settled on eliminating |
It's kinda sad that we need the
case
keyword for every case:Would love to be able to write:
Or:
(the last example is orthogonal and being discussed here)
After pattern matching gets out, I assume a good bit of code will be updated to be expressed in them, and it'd be nice to have them syntactically optimal. Apologies if there's an obvious reason this can't be done, but thought I'd raise the issue, just in case.
The text was updated successfully, but these errors were encountered: