-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Proposal: Modify sequence expression to allow scope variables like "var x = { e is int y ? y : 0 }" #15651
Comments
This'll be ambiguous in case of an array initializer. int[] x = { e is int y ? y : 0 }; |
Yes, I'm just thinking about the array initializer case while I'm posting. Sequence expression can be unambiguously written with an extra semicolon, in case we really need to differentiate: var x = { e is int y ? y : 0; }; Edit: Seriously, it is not ambigous to the compiler in your example. Because |
An possible ambiguous array initializer example: object[] x = { e is object[] y ? y : new object[0] }; I think this is very rare case. |
It's not. The ternary returns an array hence x will be a singleton array of another array (implicitly upcasted to object). Ambiguity has nothing to do with rarity as it must be resolved in the compiler anyways. |
In addition to what @alrz said this is also inconsistent you use So if this should work |
@eyalsk why do you think that way? For the other point, Without using var x = (e is int y ? y : 0;); // note the semicolon inside parenthesis. A bit confusing though. |
I know, I was referring to your suggestion.
By your suggestion this should work!
No, they aren't the same thing, there's a difference. |
Sorry I cannot understand your logic. I said |
I don't think the following is ambiguous, other than to a potentially inexperienced reader of the code: int[] x = { e is int y ? y : 0 }; It initiliases the int[] x = { { e is int y ? y : 0 } }; What am I missing? if ({e is int y}) { Use(y); } Wouldn't work as I understand this suggestion as |
Where in the world I said it would leak? There's obviously a misunderstanding here, so let me try again. What I'm saying is if you think this should work
Then I just think that it's nicer than wrapping the whole
When I said the following:
I meant that we should have only one way to narrow the scope of variables throughout the language.
Yeah I know but we need to have one way to do things so if it's possible to have these braces around the ternary operator then it might make sense to do the same and reuse it for everything else where we want to narrow the scope. 😄 |
@DavidArno It's still a valid array initializer. int[,] x = { { e is int y ? y : 0 } }; Therefore, it's still ambiguous. |
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! |
Inspiration:
In current scoping rules,
{ }
can be used to reduce scope of pattern variables and our vars like:But there is no easy way for expression statements:
If we can allow block effectively an expression, like sequence expression, then we can write:
This is inspired from discussion at #15538.
Detail Design
Base on sequence expression. I don't mind too much about
( )
vs{ }
, as long as it creates a scope for all variables created inside. But here we need to enclose one statement inside, so( single_statement )
is ambiguous on whether it is a sequence expression or just redundent parenthesis. So I think{ }
is a better choice.The text was updated successfully, but these errors were encountered: