Skip to content
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

Closed
gafter opened this issue Feb 5, 2015 · 9 comments
Closed

Proposal: Declaration Expressions #254

gafter opened this issue Feb 5, 2015 · 9 comments

Comments

@gafter
Copy link
Member

gafter commented Feb 5, 2015

One feature considered and ultimately not added to C# 6 was declaration expressions. A declaration expression is a new expression of the form

expression:
     local-variable-type local-variable-declarator

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:

argument-value:
     out local-variable-type identifier

We need to precisely define the scope of these variables, and perhaps require that they be initialized at the point of declaration.

@paulomorgado
Copy link

(Sorry, I don't know of any other way of being CCed when someone replies to this issue. I'll remove it later)

@Miista
Copy link

Miista commented Mar 9, 2015

There's a subscribe button at the top right of the page.

@paulomorgado
Copy link

@Miista, not if you have subscribed to the repo.

@airbreather
Copy link

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);

@bondsbw
Copy link

bondsbw commented Jun 24, 2015

I closed #3664 after determining it was a subset of this proposal. It may provide an additional scenario to consider.

@gafter
Copy link
Member Author

gafter commented Nov 20, 2015

See #6183 which is a subset of this but might be done sooner than this.

@alrz
Copy link
Contributor

alrz commented Dec 1, 2016

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.

@NetMage
Copy link

NetMage commented Apr 28, 2017

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?)

@gafter
Copy link
Member Author

gafter commented Apr 28, 2017

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.

@gafter gafter closed this as completed Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants