Skip to content

Conversation

BillWagner
Copy link
Member

It was hard to separate these two, so adding them together.

@BillWagner BillWagner force-pushed the feature-initializer-add branch 3 times, most recently from aea5c23 to e4e4d21 Compare October 26, 2020 20:40
> ```
> *end example*
> *Example*: Given an appropriate definition of `C`, the following example:
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

@Nigel-Ecma Nigel-Ecma Dec 16, 2020

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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

@jskeet jskeet added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Nov 18, 2020
```

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.
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor

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.

@jskeet jskeet removed the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Feb 5, 2021
@jskeet
Copy link
Contributor

jskeet commented Feb 5, 2021

(Removed label as there are suggestions/comments that haven't been addressed. Let's get that tidied up before reviewing.)

@RexJaeschke RexJaeschke added this to the C# 6 milestone Mar 17, 2021
@BillWagner BillWagner force-pushed the feature-initializer-add branch from 166d458 to 1c2f2c1 Compare March 17, 2021 14:21
An edit for await in catch and finally clauses (#3) snuck into this PR
@BillWagner BillWagner force-pushed the feature-initializer-add branch from 1c2f2c1 to f2484b9 Compare November 17, 2021 21:10
```

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.
Copy link
Contributor

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.

Copy link
Contributor

@jskeet jskeet left a 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

@BillWagner BillWagner merged commit 175ce90 into draft-v6 Dec 15, 2021
@BillWagner BillWagner deleted the feature-initializer-add branch December 15, 2021 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants