-
Notifications
You must be signed in to change notification settings - Fork 1
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
More idiomatic comparison methods #6
Comments
I am in favour of qualifiers that refer to time, i.e. |
There was discussion in the chat during today's TC39 plenary meeting that was related to this issue. Capturing feedback here. @rbuckton, feel free to expand/clarify. Thanks!
@ptomato FYI |
A small note on naming: the W3C tag guidelines recommend the use of the "is" prefix for methods which ask a boolean question about their argument. I also favor "isBefore" or "isEarlierThan" or similar. |
Another suggestion (tc39/proposal-temporal#517 (comment)) is that although it would be bad to have all Temporal types return a value from their valueOf() methods, it might be feasible to have Temporal.Instant.prototype.valueOf and Temporal.ZonedDateTime.prototype.valueOf stop throwing an exception and return a BigInt instead. (Note that would preclude ever increasing the resolution of exact times beyond nanoseconds. Although given how divisive nanoseconds has proven to be, maybe it's the case that sub-nanoseconds are just too far removed from human perception to be useful...) |
In that future where we need sub-nanosecond precision, |
Summary of discussion from tc39/proposal-temporal#1086 and meeting minutes:
The
Temporal.(type).compare()
static methods have confusing parameter order, in order for them to be able to be used as the comparator function for Array.prototype.sort().A suggestion that's come up several times is that Temporal types could have more idiomatic methods named something like
lessThan()
,earlierThan()
,isBefore()
,isSameOrAfter()
. Although these would duplicate the functionality ofcompare()
, they would make user code much easier to read.For example:
There was a work-in-progress at tc39/proposal-temporal#1074 on which a proposal in this repository can be based.
Sets of names that were proposed so far:
Advantages:
The meaning of the return value of
compare(a, b)
has been confusing since it was first introduced in C, because you have to think carefully about whether 1 means a > b or b < a. It can't be read left-to-right.An
isSame()
method could, in addition, implement equality based on the internal ISO-calendar slots of the Temporal object, without taking the calendar into account, which makes it fulfill a different function fromequals()
.Concerns:
Such an API might become useless if operator overloading is ever added to the language, at which point the comparison operators could simply be used.
Names like
greaterThan()
are closely tied to English grammar and so may only improve the issue for English speakers.A problem with names implying "before" or "after" is that due to time zones, a wall-clock time that is "greater than" another wall-clock time may not actually be "after" it. These names also wouldn't be consistent if similar methods were added to Temporal.Duration.
Prior art:
isSame('day')
time1.isAfter(time2)
Constraints / corner cases:
TBD
The text was updated successfully, but these errors were encountered: