-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Comments
dynamic a = 3,
b = "name",
c = DateTime.Now; What would IDE even show when you hover over |
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.
This is certainly something to think about, but not really a reason to eliminate the feature. Similar problems arose for |
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#? |
Right now you can use |
By the way there is a reason why it's not already implemented: http://stackoverflow.com/a/4950600/486561 |
And also note that this was a point of contention with C++ // 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. |
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. |
I have a PR out for this here: #5048 |
We've decided not to do this. The syntactic savings are tiny. |
JavaScript/TypeScript allow separating
var
declarations with a comma, where each declaration's type may differ. It makes for easily readable phrases. For example: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).The text was updated successfully, but these errors were encountered: