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

How is data passed into the calendar methods? #354

Closed
sffc opened this issue Feb 13, 2020 · 6 comments · Fixed by #624
Closed

How is data passed into the calendar methods? #354

sffc opened this issue Feb 13, 2020 · 6 comments · Fixed by #624
Assignees
Labels
calendar Part of the effort for Temporal Calendar API has-consensus

Comments

@sffc
Copy link
Collaborator

sffc commented Feb 13, 2020

With the conclusions from #290/#291 about the data model, it raises questions about how calendar methods receive and return data. The problem is illustrated by the following code, which, by the semantics in #290/#291, is an infinite loop:

const gregorian = {
  day: function(obj) {
    return obj.day;
  }
};

console.log(Temporal.Date.from({ calendar: gregorian }).day);

The calendar must have a way to get at the data in the isoYear/isoMonth/isoDay internal slots. We could do this by:

  1. Adding public getters for those slots to Temporal.[Date][Time].prototype.
  2. Passing raw objects to the calendar functions.
  3. Adding Temporal.[Date][Time].prototype.raw, which is an immutable raw object containing the underlying data (isoYear, isoMonth, isoDay, etc).

With option 2, for example, the calendar methods would be passed an object with fields isoYear, isoMonth, isoDay, hour, minute, second, and so on. They would either return an option bag of the same format, or return a Temporal object created with the constructor. Returning the option bag has some ergonomic advantages, because the options bag can be mutated and then returned from the function. If we wanted to return a Temporal object, we should consider allowing the constructor to accept the option bag described above (with isoYear instead of year, etc).

Related: we need a .from() method in Temporal.Calendar in order to receive requests from the .from() method on the Temporal types. I suspect the implementation would be similar to the current toISO() function we have in there.

@sffc sffc added the calendar Part of the effort for Temporal Calendar API label Feb 13, 2020
@ptomato
Copy link
Collaborator

ptomato commented Mar 26, 2020

Meeting, March 26: No general consensus yet, but we discussed some more details of option 2. In order to implement #391 the constructors of YearMonth and MonthDay would need to accept a third parameter as well as the calendar. If we chose option 2, then the consensus would be to make these parameters optional and place them after the calendar, e.g. new Temporal.MonthDay(isoMonth, isoDay, calendar = ..., refIsoYear = 2000) and new Temporal.YearMonth(isoYear, isoMonth, calendar = ..., refIsoDay = 1).

@ptomato
Copy link
Collaborator

ptomato commented Mar 27, 2020

To continue the discussion, I'm personally in favour of option 2 because I think it has the least effect on the API. My preference would be to change the API as little as possible so that non-calendar users of Temporal are not mystified by API parameters that are not relevant to them.

@sffc
Copy link
Collaborator Author

sffc commented Mar 27, 2020

I have a preference for option 1.

  • It's weird if you can set these slots in the constructor but then can't access them again later.
  • Adding .isoYear/.isoMonth/.isoDay getters does not hurt ergonomics. It adds additional options that programmers will see in MDN.

No matter which option we choose, the correct MDN for the year/month/day getters should read,

.year returns the year portion of this date according the date's calendar system. This getter calls the .year function of the calendar field.

And similar for month and day. These are the semantics we already reached consensus on in previous issues such as #291. Not stating the calendar semantics of these getters in the MDN would be withholding key information, making it much more confusing to reason about the behavior when calendar systems are in play.

Option 1 in this thread would be adding new getters whose MDN is,

.isoYear returns the year portion of this date according to the ISO calendar system, as set in the constructor.

I find both of the getters perfectly clear and sensible.

@ptomato
Copy link
Collaborator

ptomato commented Apr 3, 2020

Meeting, Apr 2: The consensus is to go with option 3 as a middle ground between 1 and 2. It allows access to the information while at the same time is less likely to be misused by programmers who actually want the non-ISO getters. Instead of a date.raw getter we are making it a method date.getISOFields(), for consistency with date.getFields() (#403).

@sffc suggested after the meeting that unlike the object returned from getFields, we may want to allow this object to be immutable, for optimization purposes.

ptomato added a commit that referenced this issue May 7, 2020
@littledan littledan added this to the Stage 3 milestone May 14, 2020
@sffc sffc self-assigned this May 20, 2020
@ryzokuken ryzokuken removed this from the Stage 3 milestone May 20, 2020
@littledan
Copy link
Member

We've settled on getISOFields() as the mechanism here; the remaining task is to document in calendar-draft.md, which is nice to have.

@ptomato
Copy link
Collaborator

ptomato commented May 26, 2020

I forgot when discussing this the other day that we do actually have to implement the method as well, and it's a prerequisite for calendar support; so it's not just nice to have. Moving this into the Stable API milestone.

ptomato added a commit that referenced this issue May 26, 2020
This is in order to be able to get the underlying fields from the data
model, which are stored in the ISO 8601 calendar.

It's not expected to be used in normal Temporal usage, it really exists
only for calendar implementors.

Closes: #354
ptomato added a commit that referenced this issue May 28, 2020
This is in order to be able to get the underlying fields from the data
model, which are stored in the ISO 8601 calendar.

It's not expected to be used in normal Temporal usage, it really exists
only for calendar implementors.

Closes: #354
ptomato added a commit that referenced this issue May 28, 2020
This is in order to be able to get the underlying fields from the data
model, which are stored in the ISO 8601 calendar.

It's not expected to be used in normal Temporal usage, it really exists
only for calendar implementors.

Closes: #354
ptomato added a commit that referenced this issue May 28, 2020
This is in order to be able to get the underlying fields from the data
model, which are stored in the ISO 8601 calendar.

It's not expected to be used in normal Temporal usage, it really exists
only for calendar implementors.

Closes: #354
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
calendar Part of the effort for Temporal Calendar API has-consensus
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants