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

Allow comma separated var declarations #4294

Closed
drewnoakes opened this issue Aug 2, 2015 · 9 comments
Closed

Allow comma separated var declarations #4294

drewnoakes opened this issue Aug 2, 2015 · 9 comments
Labels
Area-Language Design Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on.

Comments

@drewnoakes
Copy link
Member

JavaScript/TypeScript allow separating var declarations with a comma, where each declaration's type may differ. It makes for easily readable phrases. For example:

var radius = 3,
    point = GetPoint(),
    x = radius*2*point.X + radius,
    y = radius*2*point.Y + radius;

Right now comma separated declarations only seem to work when the type is specified explicitly, in which case it is restricted to being identical amongst all declared variables.

This is just a syntax thing. In my opinion, the above code is easier to read. The compiler exhibits no preference.

This request applies equally to the proposed readonly locals (#155), whatever keyword is decided upon (val/let/readonly var/etc).

@dsaf
Copy link

dsaf commented Aug 3, 2015

var in C# is completely different from var in JavaScript/TypeScript. C# has JavaScript's var as dynamic so you can do what JavaScript does already today:

dynamic a = 3,
        b = "name",
        c = DateTime.Now;

What would IDE even show when you hover over var?

@sharwell
Copy link
Member

sharwell commented Aug 3, 2015

It makes for easily readable phrases.

This is an interesting proposal. I actually believe the opposite for this (i.e. that it makes for especially unreadable phrases). However, if the syntax were allowed then StyleCopAnalyzers could be updated to identify cases where the syntax was used and automatically convert it to another form. The overall advantage would be reducing the amount of typing required to input certain programs.

What would IDE even show when you hover over var?

This is certainly something to think about, but not really a reason to eliminate the feature. Similar problems arose for nameof (especially regarding CodeLens and rename refactorings), but they worked themselves out over time.

@dsaf
Copy link

dsaf commented Aug 3, 2015

There is one short step from that to this:

public class Sample
{
    public var Name { get; } = "Abcde";
    public var Properties { get; } = new Dictionary<string, object>();
}

Is it the right direction for C#?

@sharwell
Copy link
Member

sharwell commented Aug 3, 2015

There is one short step from that to this:

Right now you can use var for local variables, but not for fields constants. It seems like using it for properties would be quite a long step away, and also an orthogonal request.

@dsaf
Copy link

dsaf commented Aug 3, 2015

By the way there is a reason why it's not already implemented: http://stackoverflow.com/a/4950600/486561

@HaloFour
Copy link

HaloFour commented Aug 3, 2015

And also note that this was a point of contention with C++ auto. They did implement multiple declarations but all of the variables must always deduce to the same type. The type inference won't even consider common base classes:

// given Bar : Foo and Baz : Foo
// ERROR C3538 in a declarator-list 'auto' must always deduce to the same type
// WARNING 'auto' type is "Bar" for this entry, but was previously implied to be "Foo"
// WARNING 'auto' type is "Baz" for this entry, but was previously implied to be "Foo"
auto foo = Foo(), bar = Bar(), baz = Baz();

I'd have to agree with Eric Lippert. Allowing the syntax would produce confusion as a decent percentage of people would expect whatever was opposite to the implemented behavior.

@jveselka
Copy link

jveselka commented Aug 4, 2015

You can implements the most strict option and no confusion can arise. "All variables in declaration must be initialized to the same type with no inferrence allowed." It still has value over prohibiting comma separated var declaration altogether.

var a = 1, b = "2"; //not OK
var c = 3, d = 4.0; //not OK
var e = 5, f = 6; //OK
var g = 7, h = 2147483648; //not OK (int and uint)
var i = 9u, j = 2147483648; //OK

Meaning of code is super-obvious and all attempts for misuse fail at decalration.

@CyrusNajmabadi
Copy link
Member

I have a PR out for this here: #5048

@gafter gafter added 4 - In Review A fix for the issue is submitted for review. and removed 4 - In Review A fix for the issue is submitted for review. labels Sep 8, 2015
@gafter
Copy link
Member

gafter commented Oct 5, 2015

We've decided not to do this. The syntactic savings are tiny.

@gafter gafter added the Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on. label Oct 5, 2015
@gafter gafter closed this as completed Oct 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Won't Fix A real bug, but Triage feels that the issue is not impactful enough to spend time on.
Projects
None yet
Development

No branches or pull requests

8 participants