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

Comments

Projects
None yet
7 participants
@gafter
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

This comment has been minimized.

Show comment
Hide comment
@paulomorgado

paulomorgado Mar 9, 2015

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

paulomorgado commented Mar 9, 2015

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

@Miista

This comment has been minimized.

Show comment
Hide comment
@Miista

Miista Mar 9, 2015

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

Miista commented Mar 9, 2015

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

@paulomorgado

This comment has been minimized.

Show comment
Hide comment
@paulomorgado

paulomorgado Mar 9, 2015

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

paulomorgado commented Mar 9, 2015

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

@airbreather

This comment has been minimized.

Show comment
Hide comment
@airbreather

airbreather May 5, 2015

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

airbreather commented May 5, 2015

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

This comment has been minimized.

Show comment
Hide comment
@bondsbw

bondsbw Jun 24, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@gafter

gafter Nov 20, 2015

Member

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

Member

gafter commented Nov 20, 2015

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

@alrz

This comment has been minimized.

Show comment
Hide comment
@alrz

alrz Dec 1, 2016

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@NetMage

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

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

This comment has been minimized.

Show comment
Hide comment
@gafter

gafter Apr 28, 2017

Member

Declaration expressions, as added in C# 7, are discussed in dotnet/csharplang#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.

Member

gafter commented Apr 28, 2017

Declaration expressions, as added in C# 7, are discussed in dotnet/csharplang#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 Apr 28, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment