Replies: 8 comments 96 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I think we can just spell out the new scope to break from - basically "break expression value;", or "break switch value;" if you like. While |
Beta Was this translation helpful? Give feedback.
-
I think the fact that you'd get an error on misusages can address teh confusion int m;
switch (e) {
case a: M(); m = 1; break;
case b: M(); m = 2; break;
default: M(); m = 3; break;
} There's really no other way to do this even if it's inside a loop or there's some artibrary return statements, int m = e switch {
a => { M(); break 1; }, // could simplify to `{ M(); 1 },` when there's a single break at the end
b => { M(); break 2; },
_ => { M(); break 3; },
} Something that is left out of the discussion is the same improvement for switch statements, switch (e) {
case a => { M(); m = 1; };
case b => { M(); m = 2; };
default => M();
} I think that is discussed in #3038 but supposedly all would be built on top of block-expressions even if they wouldn't be allowed anywhere else. |
Beta Was this translation helpful? Give feedback.
-
I definitely don't like |
Beta Was this translation helpful? Give feedback.
-
It'll be interesting to see what use cases the team come up with for these block-bodied expressions. From experience, the only time I wish such a thing existed is when I want to perform some sort of assignment or deconstruct and then perform some function on those new variables. So for me, the value would always be at the end of the block and therefore Others likely have different experiences though, so I'm intrigued where this feature goes next. |
Beta Was this translation helpful? Give feedback.
-
I am still in favor of out and don't quite understand how this could be confusing. out is today only allowed in for parameters so having it inside a block would be completely new and couldn't be confused for anything else. as a reviewer looking at code that uses either of yield, break or return in side a block could be confusing and I would need to look very deeply into the code to understand what those are referring to, but out on the other hand would be a no-brainer because it couldn't be used like this before, so there is only one thing this could mean, no ambiguity, no misunderstanding. anyhow, I appreciate that out Was considered by the ldm, though I can't fully understand the confusion there. |
Beta Was this translation helpful? Give feedback.
-
To attack from a different angle: void Foo(int a, int b)
{
var bar = ((Func<int>)(() => { Baz(); return a + b; }))();
...
} If we clean up the clutter (as the lambda is immediately invoked and discarded), it would ideally be something like: void Foo(int a, int b)
{
var bar = () => { Baz(); return a + b; }(); // not legal today
...
} Essentially, it can still be treated as a lambda, so For switch expression, it would be like: var bar = a switch {
0 => { Baz(); return a + b; }(); // not legal today
...
} To use it outside of void Foo(int a, int b)
{
var bar = { Baz(); return a + b; }(); // not legal today
...
} |
Beta Was this translation helpful? Give feedback.
-
Borrowing a leaf from raw string literals, the number of curly braces introducing the sequence expression could tell you how many parens to wrap the expression in that becomes the result. var x =
{{{
using var y = ...;
(((y.Z)))
}}}; (((🙃))) |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-08-28.md
Agenda
ref
ternariesBeta Was this translation helpful? Give feedback.
All reactions