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

Should field properties be enumerable? #403

Closed
gibson042 opened this issue Feb 24, 2020 · 18 comments · Fixed by #520
Closed

Should field properties be enumerable? #403

gibson042 opened this issue Feb 24, 2020 · 18 comments · Fixed by #520
Assignees
Labels
behavior Relating to behavior defined in the proposal has-consensus

Comments

@gibson042
Copy link
Collaborator

It would be nice to have e.g. Object.assign({}, Duration.from("PT42S")).seconds === 42.

@Ms2ger
Copy link
Collaborator

Ms2ger commented Feb 25, 2020

I would be inclined to match https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects.

@ryzokuken ryzokuken added meeting-agenda non-prod-polyfill THIS POLYFILL IS NOT FOR PRODUCTION USE! spec-text Specification text involved behavior Relating to behavior defined in the proposal and removed non-prod-polyfill THIS POLYFILL IS NOT FOR PRODUCTION USE! spec-text Specification text involved labels Feb 25, 2020
@gibson042
Copy link
Collaborator Author

That section says that built-in data properties are not enumerable "unless otherwise specified", but I'm suggesting that we do specify otherwise because instances of the Temporal types are basically struct-like records very similar to objects with well-known property names and it would be convenient to preserve that similarity.

Just like the index properties of strings and arrays are enumerable, so too should be the properties of Temporal type instances that hold their structured data.

@littledan
Copy link
Member

About matching existing conventions, these feel pretty similar to RegExp properties like global to me. (I believe these are non-enumerable.) Or maybe we could consider that a mistake, and try to establish new conventions?

@ptomato
Copy link
Collaborator

ptomato commented Feb 27, 2020

If we go in the direction of considering objects with well-known property names to be equivalent to Temporal objects, should we go so far as to remove some of the checks that we have in the spec for the [[InitializedTemporalX]] slot? e.g. allow things like date.compare({year: 2020, month: 2, day: 27}) after all? or even Temporal.DateTime.inTimeZone.call({year: 2020, month: 2, day: 27, hour: 10, minute: 27}, 'UTC')?

@Ms2ger
Copy link
Collaborator

Ms2ger commented Feb 28, 2020

It would be nice to have e.g. Object.assign({}, Duration.from("PT42S")).seconds === 42.

Note that this will not work even if we make the fields enumerable; Object.assign only takes own properties into account, and these must be on the prototype.

@gibson042
Copy link
Collaborator Author

gibson042 commented Feb 29, 2020

I don't know if it's fair to say that they must be on the prototype, but that is nonetheless a good point. The above becomes much less compelling when we need to add in Object/Reflect static methods. :\

@ptomato
Copy link
Collaborator

ptomato commented Feb 29, 2020

This would seem to be a good argument in favour of adding a Duration.with method; because Duration.from({...otherDuration, seconds: 42}) wouldn't work.

@ryzokuken ryzokuken added the needs plenary input Needs to be presented to the committee and feedback incorporated label Mar 5, 2020
@ptomato
Copy link
Collaborator

ptomato commented Mar 5, 2020

Meeting, Mar. 5: The intention is to make the core properties enumerable, and allow calendars to add additional enumerable properties such as era. However, this is something we should get input on when presenting at the March TC39 meeting.

@ljharb
Copy link
Member

ljharb commented Mar 6, 2020

Are these own data properties? I'd expect them to be accessors on the prototype, which would make their enumerability irrelevant for most use cases (object spread, Object.assign, Object.keys/entries/values, etc).

@ptomato
Copy link
Collaborator

ptomato commented Mar 18, 2020

We did discuss that they would need to be own properties in order to be enumerable, but that deviates from an existing ES convention which is why we wanted input from the plenary on this.

@ljharb
Copy link
Member

ljharb commented Mar 18, 2020

They can be enumerable without being own, but they have to be both to be included in assign/spread.

Is this on the agenda yet?

@ptomato
Copy link
Collaborator

ptomato commented Mar 18, 2020

I believe @pipobscure already put it on the agenda.

@jasonwilliams
Copy link
Member

Does this still need plenary input?

@ptomato
Copy link
Collaborator

ptomato commented Mar 27, 2020

I think so, yes.

@littledan
Copy link
Member

it'd be great to have the notes landed so they can be easily referenced by the plenary, cc @ryzokuken

@sffc
Copy link
Collaborator

sffc commented Mar 30, 2020

Note extracted from #487:

The Japanese calendar might choose to make .era enumerable, whereas other calendars might not. This is because for the Japanese calendar, there are 4 fields that fully define a date: era, year, month, and day. In other words, the set of fields that are enumerable should be calendar-dependent.

@gibson042
Copy link
Collaborator Author

A possibility raised by a few different parties in plenary is to have a method for returning plain snapshots of the fields, e.g. toJSON (which currently serializes to string) or toObject, which would avoid language fit concerns at the expense of maximal brevity.

@ptomato ptomato added has-consensus and removed needs plenary input Needs to be presented to the committee and feedback incorporated labels Apr 3, 2020
@ptomato
Copy link
Collaborator

ptomato commented Apr 3, 2020

Meeting, Apr 2: Consensus is that the properties should not be own properties. Instead we will add a getFields() method, which returns a new plain JavaScript object, with all the fields as enumerable, writable, own data properties.

The code snippet from the OP would become:

Object.assign({}, Duration.from("PT42S").getFields()).seconds === 42

@ptomato ptomato self-assigned this Apr 21, 2020
ptomato added a commit that referenced this issue Apr 21, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue Apr 23, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue Apr 30, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue Apr 30, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue May 7, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue May 11, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue May 11, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue May 13, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
ptomato added a commit that referenced this issue May 13, 2020
getFields() is how to get an object with enumerable own data properties.

Closes: #403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
behavior Relating to behavior defined in the proposal has-consensus
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants