C# 8.0 switch expressions only work with single return statements. #2181
-
Just a thought, what if instead of slowly trickling half-baked features of pattern matching, one consistent and exhaustive solution was implemented from the get-go? Been recently reading Mads blog on MSDN and toying around with the latest C# 8 beta I could help but wonder:
I'm sorry for the down note but I'm really questioning the decisions you've made, even syntactically |
Beta Was this translation helpful? Give feedback.
Replies: 22 comments 2 replies
-
This is not the case. 'switch expressions' work anywhere you can currently use an expression.
Multi-line statements are not banned.
Nothing has been made obsolete.
Because there is already a switch-statement, and we wanted a statement form to allow patterns. We also wants an expression form. The statement form was kept mostly the same as how you always write it. i.e.
Maybe. But the decision was made that there's practically no benefit to using that keyword. we already have 'switch', and it means the right thing here, so why nto use htat? |
Beta Was this translation helpful? Give feedback.
-
Then don't use it. It's like someone who doesn't like |
Beta Was this translation helpful? Give feedback.
-
Yes. The grammar for expression-bodied members is: ```=> expr ;` That's why you have to write This just follows that same rule. This is necessary as you could otherwise get into ambiguous situations. If you don't have the semicolon, then the next token might be viewed to be a followup token for your member's expression body. |
Beta Was this translation helpful? Give feedback.
-
Note: for general discussions about hte language, i recommend gitter.im/dotnet/csharplang. it's a place more suited to discussion like this. Cheers! |
Beta Was this translation helpful? Give feedback.
-
You can use "switch expressions" anywhere you can use an expression: var greeting = language switch {
"eng" => "Hello",
"spa" => "Hola"
};
Multi-line statements aren't expressions. There is a separate proposal which looks into making sequences of statements result in an expression which will enable this scenario. Otherwise, a local function will have to suffice for the time being.
No, the existing
Pattern matching has nothing to do with if (object is Point { X: 0, Y: 0 } ) {
WriteLine("origin");
}
else if (object is Point { X: var x, Y: var y }) {
WriteLine($"({x}, {y})")
}
else {
WriteLine("unknown");
}
That keyword was on the table. So were about a dozen other permutations of the syntax. It was decided to be better to reuse the existing keyword and to make it function like an operator.
Yes, when using public int Add(int x, int y) => x + y; // this semicolon is still necessary |
Beta Was this translation helpful? Give feedback.
-
Are you sure? Then why am I getting those squiggles? |
Beta Was this translation helpful? Give feedback.
-
@xCyborg This is not a switch expression. This is the old statement form of the switch. |
Beta Was this translation helpful? Give feedback.
-
I meant more like |
Beta Was this translation helpful? Give feedback.
-
Yes that's what I meant, just like in Rust. |
Beta Was this translation helpful? Give feedback.
-
OK, thanks for the clarifications, I'll look for the proposal. |
Beta Was this translation helpful? Give feedback.
-
Try this: public void Method(object mysteryPlanet) {
mysteryPlanet switch {
case Planet { index: 3 } => Console.WriteLine("Its indeed the Earth"),
case Planet => Console.WriteLine("Its indeed a planet"),
_ => Console.WriteLine("Not a planet")
};
} or you can also do this: public void Method(object mysteryPlanet) {
switch (mysteryPlanet) {
case Planet { index: 3 }:
Console.WriteLine("Its indeed the Earth");
break;
case Planet _:
Console.WriteLine("Its indeed a planet");
break;
default:
Console.WriteLine("Not a planet");
break;
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I will say that it was very difficult for me to find docs for this syntax yesterday via Google, Microsoft and csharplang. Sharplab doesn't seem to have a branch to play with it and master didn't seem to work. I eventually got it by trial and error and finding my way eventually to the section in the patterns proposal. |
Beta Was this translation helpful? Give feedback.
-
Last I checked sharplab master worked |
Beta Was this translation helpful? Give feedback.
-
Looks like a lot of branches got dropped by SharpLab. There used to be a branch for recursive patterns where switch expressions worked. Would be nice to have a branch that represents the preview. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hmmm. I somehow got it to work just over a week ago: #2155 (comment) I wish I could remember how... |
Beta Was this translation helpful? Give feedback.
-
As per #2095, statement expressions are being considered for adding to the set of valid expressions within a switch expression and the switch expression will thus be a statement expression (though I'm sure that should be an expression statement) too. Also, as the meeting notes where that was discussed say, "We like it. Not sure it will make it for C# 8.0.", thus why, static void Display(object o) => o switch
{
Point { X: 0, Y: 0 } => WriteLine("origin"),
Point { X: var x, Y: var y } => WriteLine($"({x}, {y})"),
_ => WriteLine("unknown")
}; doesn't work in VS2019 P2. It should (hopefully) work in a future version of C# and VS 2019. |
Beta Was this translation helpful? Give feedback.
-
So cool, even if doesn't land in C# 8.0 I hope it will land in a later dot release. static void Display(object o) {
switch (o)
{
Point { X: 0, Y: 0 } => WriteLine("origin"),
Point { X: var x, Y: var y } => WriteLine($"({x}, {y})"),
_ => WriteLine("unknown")
}
} |
Beta Was this translation helpful? Give feedback.
-
The work to implement this is being tracked at dotnet/roslyn#30649 |
Beta Was this translation helpful? Give feedback.
-
Multi line switch expressions, and enhanced switch statements championed at #3038 |
Beta Was this translation helpful? Give feedback.
-
Hi I didn't get it, if I have multiple operations under same case, for example:
so how can I switch it to C# 8-9 switch expression? Thank you very much.. |
Beta Was this translation helpful? Give feedback.
This is not the case. 'switch expressions' work anywhere you can currently use an expression.
Multi-line statements are not banned.
Nothing has been made obsolete.
Because there is already a switch-statement, and we wanted a statement form to allow patterns. We also wants an expression form. The statement form was kept mostly the same as how…