Champion: Switch expression as a statement expression #8925
Replies: 68 comments
-
Just to check, since I get confused between statement expressions and expression statements, will the following syntax by valid with this proposal: void M(bool c, ref int x, ref string s) => c switch { true => x = 1, false => s = null }; |
Beta Was this translation helpful? Give feedback.
-
Is the operator for default case still the same for this scenario or this new feature?? As I understand underscore is the current operator
|
Beta Was this translation helpful? Give feedback.
-
Together with exhaustiveness checks this will render existing switch statement pretty much legacy? |
Beta Was this translation helpful? Give feedback.
-
@dsaf how so? You still can't have things like blocks/multi-statements in an expression-form switch. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi well maybe initially but those could be added going forward. |
Beta Was this translation helpful? Give feedback.
-
Non-exhaustive switches can't be written via switch expression, void M(string s) {
switch (o) {
case 1: s = "A"; break;
case 2: s = "B"; break;
}
} Unless this is allowed void M(string s) {
o switch { 1 => s = "A", 2 => s = "B" };
} (You still need to repeat the assignment.) |
Beta Was this translation helpful? Give feedback.
-
In that case, they do not "render existing switch statement pretty much legacy". :) |
Beta Was this translation helpful? Give feedback.
-
What happen to var a = c switch { true => x = 1, false => s = null }; ? |
Beta Was this translation helpful? Give feedback.
-
It seems sensible that it'll give you a |
Beta Was this translation helpful? Give feedback.
-
@DavidArno We actually could write this int x = 0;
var a = x = 1; // a is int And in the next C# I think we could possible to var a = condition ? 1 : null; // a become Nullable<int> So actually I expect that it might be possible to var a = c switch { true => x = 1, false => s = null };
// a become Nullable<int> or object or (int | string)
// then assign both x and a to 1 if true else assign both s and a to null So I just ask to make sure |
Beta Was this translation helpful? Give feedback.
-
If If |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h It possible that it's behaviour could be As for me I hope that we should support union type and could let this syntax return union type. Or just plain |
Beta Was this translation helpful? Give feedback.
-
The value of, void Foo() {}
void Bar() => Foo();
void Baz()
{
var x = Foo(); // results in error CS0815: Cannot assign void to an implicitly-typed variable
} So the same should be true of a statement expression switch expression, void Bar(bool c, ref int x, ref string s)
=> c switch { true => x = 1, false => s = null }; // all good
void Baz(bool c, ref int x, ref string s)
{
var a = switch { true => x = 1, false => s = null };
// results in error CS0815: Cannot assign void to an implicitly-typed variable
} |
Beta Was this translation helpful? Give feedback.
-
c switch { true => 1, false => null }; This expression should be var a = c switch { true => 1, false => null }; // a is int? Given that var a = x = 1; // a is int Expression C# also already has the ability to ignore any return type to int Bar() => 0;
void Foo() => Bar(); So I don't think we need to limit |
Beta Was this translation helpful? Give feedback.
-
Sure, c switch { true => x = 1, false => s = null }; where |
Beta Was this translation helpful? Give feedback.
-
I'm not certain what was unclear about my last post. If you want to discuss meta points like these, start a new discussion |
Beta Was this translation helpful? Give feedback.
-
This would be super handy in unit tests with most result switch
{
int i => Assert.Equal(i, 5),
string s => Assert.Equal(s, "x"),
_ => Assert.Null(result),
} |
Beta Was this translation helpful? Give feedback.
-
I like that idea. Viewing the switch statement as a state machine, it makes sense that I would sometimes want to execute code that does not have a return statement. |
Beta Was this translation helpful? Give feedback.
-
Yeah, the issue that you mention looks different to me. First, why don't you use "typeof" to determine what type you are evaluating? Then you use a switch case. |
Beta Was this translation helpful? Give feedback.
-
Can you demonstrate how |
Beta Was this translation helpful? Give feedback.
-
Maybe I misunderstood that creepy thing... What it does look to me is receiving a generic T, and depending on T type it process something on that type.... am I wrong? Anyway, this is really creepy, looks like the typical Python crap. |
Beta Was this translation helpful? Give feedback.
-
@ccarlo88 Switch expressions receive a value and allow patterns to match against it. That's been in the language a while now. This issue is the request for allowing the args of the switch expression to be void expressions, and for the switch expression to be considered a legal statement-expression (like Assert.Equal is). |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
-
@ccarlo88 please keep things on-topic and following the .net code of conduct. WRT the language, the group that designs the language (the LDM) is the same one that has been designing it for 25 years now, with most of the same people involved over all that time. |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
-
@ccarlo88 please keep things on-topic and following the .net code of conduct. If you have feelings beyond that, please take them to some other outlet (a blog or discord channel would be better). |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
This comment has been hidden.
This comment has been hidden.
-
Oh, cool, I was literally just about to ask if this could be unlocked. |
Beta Was this translation helpful? Give feedback.
-
I propose that we support a switch expression as a statement expression when every arm's expression is also a statement expression. No common type among the arms is required when used as a statement expression.
Meeting notes
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-08-31.md#switch-expression-as-a-statement
Beta Was this translation helpful? Give feedback.
All reactions