-
Notifications
You must be signed in to change notification settings - Fork 93
C# 6.0 feature: collection (index) initializers, and extension add method for initializers. #8
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
Conversation
aea5c23
to
e4e4d21
Compare
standard/expressions.md
Outdated
> ``` | ||
> *end example* | ||
> *Example*: Given an appropriate definition of `C`, the following example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "appropriate definition" here presumably includes:
either an init which creates an empty array for z
, and object for y
, etc.
or the generated new
calls are not shown in the equivalent series of assignments
Does this need to be made clearer, corrected, or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't think that's required for the example - the example will throw an exception if executed and the initializer doesn't create an empty array for z
etc, but when the point is to say "It's equivalent to this code" I don't think we need to care about what happens at execution time beyond the translation.
Given that this is only informative, I think it's reasonable not to specify what "appropriate" means.
I do wonder whether we should use X
, Y
and Z
as more idiomatic names though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm not sure is clear is (taking from the example below) when it is valid/required to write:
phoneNumbers = { "206-555-0101", "425-882-8080" };
vs.
phoneNumbers = new List<string> { "206-555-0101", "425-882-8080" };
for the cases when phoneNumbers
is a local/global variable, field of an object, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re: x
vs. X
: are these not field names which conventionally start with lowercase? See the next example with phoneNumbers
and PhoneNumbers
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most code conventions require fields to be private, which means this would only be valid inside the code for C. I very, very rarely see object initializers used to initialize fields - they're almost always used with properties in my experience. While it isn't "wrong" to show fields, I think it would be more helpful to show the more common usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Will discuss the other part in the meeting... out of time now :)
``` | ||
|
||
An object initializer consists of a sequence of member initializers, enclosed by `{` and `}` tokens and separated by commas. Each member initializer shall name an accessible field or property of the object being initialized, followed by an equals sign and an expression or an object initializer or collection initializer. It is an error for an object initializer to include more than one member initializer for the same field or property. It is not possible for the object initializer to refer to the newly created object it is initializing. | ||
An object initializer consists of a sequence of member initializers, enclosed by `{` and `}` tokens and separated by commas. Each *member_initializer* shall designate a target for the initialization. An *identifier* shall name an accessible field or property of the object being initialized, whereas an *argument_list* enclosed in square brackets shall specify arguments for an accessible indexer on the object being initialized. It is an error for an object initializer to include more than one member initializer for the same field or property. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth explicitly noting that it is okay to specify multiple member initializers that use indexers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given it’s an error if the same field or property is initialised twice then yes something should be said about indexers – but what are the rules? Are the argument_list’s required to be distinct (as with fields/properties) or can the same indexed value be initialised more than once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meeting notes: Yes, we want to spell this out, but we can do so in a follow-up PR.
(Removed label as there are suggestions/comments that haven't been addressed. Let's get that tidied up before reviewing.) |
166d458
to
1c2f2c1
Compare
An edit for await in catch and finally clauses (#3) snuck into this PR
1c2f2c1
to
f2484b9
Compare
``` | ||
|
||
An object initializer consists of a sequence of member initializers, enclosed by `{` and `}` tokens and separated by commas. Each member initializer shall name an accessible field or property of the object being initialized, followed by an equals sign and an expression or an object initializer or collection initializer. It is an error for an object initializer to include more than one member initializer for the same field or property. It is not possible for the object initializer to refer to the newly created object it is initializing. | ||
An object initializer consists of a sequence of member initializers, enclosed by `{` and `}` tokens and separated by commas. Each *member_initializer* shall designate a target for the initialization. An *identifier* shall name an accessible field or property of the object being initialized, whereas an *argument_list* enclosed in square brackets shall specify arguments for an accessible indexer on the object being initialized. It is an error for an object initializer to include more than one member initializer for the same field or property. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meeting notes: Yes, we want to spell this out, but we can do so in a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remaining actions for @BillWagner:
- Resolve the conflict
- Squash and merge
It was hard to separate these two, so adding them together.