-
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: Declaration Expressions #254
Comments
(Sorry, I don't know of any other way of being CCed when someone replies to this issue. I'll remove it later) |
There's a subscribe button at the top right of the page. |
@Miista, not if you have subscribed to the repo. |
Hi, I'd just like to chime in my support for this feature and note some existing syntax that having something like this may enable it to look much nicer / cleaner, depending on how this goes... guessing you've probably seen something like this before, but might as well: IEnumerable<Foo> fooSequence = new Foo[0]; // just for demo
int someThreshold = 110; // just for demo
// ...
fooSequence = fooSequence.Where(foo =>
{
int parsedBar;
return Int32.TryParse(foo.BarString, out parsedBar) &&
parsedBar < someThreshold;
}); versus this, sans curly braces: IEnumerable<Foo> fooSequence = new Foo[0]; // just for demo
int someThreshold = 110; // just for demo
// ...
fooSequence = fooSequence.Where(foo => Int32.TryParse(foo.BarString, out int parsedBar) &&
parsedBar < someThreshold); I can get it to look almost the same without declaration expressions, but it's subtly broken in certain circumstances (well, not literally as written, but if the IEnumerable<Foo> were instead implicitly typed with "var" and there was a .AsParallel() or something): IEnumerable<Foo> fooSequence = new Foo[0]; // just for demo
int someThreshold = 110; // just for demo
// ...
int parsedBar;
fooSequence = fooSequence.Where(foo => Int32.TryParse(foo.BarString, out parsedBar) &&
parsedBar < someThreshold); |
I closed #3664 after determining it was a subset of this proposal. It may provide an additional scenario to consider. |
See #6183 which is a subset of this but might be done sooner than this. |
I assume that we can use declaration expressions also in ref args. :) F(ref object x = null); So there would be no assumption on the default value passed to the argument. |
It is really hard to tell where some of these proposals go it seems - there are no closing or summary notes on closed issues, and I assume an Open issue means (e.g. general declaration expressions) wasn't included in C# 7.0, but it seems like I should have to go to the C# design notes to tell. Perhaps it shouldn't be possible to close an issue without putting in a comment. (BTW, is there a meta discussion area this really should be in?) |
Declaration expressions, as added in C# 7, are discussed in https://github.com/dotnet/csharplang/issues/365 . Related to this is a proposal for sequence expressions at dotnet/csharplang#377 , which is not currently championed in the LDM. Closing this issue. |
One feature considered and ultimately not added to C# 6 was declaration expressions. A declaration expression is a new expression of the form
Such an expression resolves to an lvalue for a fresh local variable that is definitely assigned only if the declarator contains an initializer. In addition, we would support fresh variables for
out
arguments:We need to precisely define the scope of these variables, and perhaps require that they be initialized at the point of declaration.
The text was updated successfully, but these errors were encountered: