-
Notifications
You must be signed in to change notification settings - Fork 156
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
size-suggestion: Fold some withX()
functions into with()
functions
#2847
Comments
Yeah, these are just not redundant with each other even if they are similarly named. I'd be willing to reluctantly consider removing |
My origional suggestion could be found in https://docs.google.com/document/d/16Rd43XB8z6iLC2c9AfF--unDFg_t476tI1PFe7PbbcM/
you can have
where what need to be a better name to indicate what kind of object the with() is, just as what was indicated by the suffix in the method. |
Agreed.
We could do something like that (or require that |
From a readability and ergonomics point of view, this seems harmless: // Current:
.withPlainDate("2024-05-20")
.withPlainTime("00:00")
// Proposed:
.with(Temporal.PlainDate.from("2024-05-20"))
.with(Temporal.PlainTime.from("00:00"))
// Alternative:
.with({ date: "2024-05-20" })
.with({ time: "00:00" }) As far as // Current:
.withTimeZone("America/Chicago")
.withCalendar("buddhist")
// Proposed:
.with(Temporal.TimeZone.from("America/Chicago"))
.with(Temporal.Calendar.from("buddhist"))
// Alternative:
.with({ timeZone: "America/Chicago" })
.with({ calendar: "buddhist" }) All of the following would just throw an exception: .with({ date: "2024-05-20", monthCode: "M06" })
.with({ time: "00:00", hour: 12 })
.with({ timeZone: "America/Chicago", hour: 12 })
.with({ calendar: "buddhist", monthCode: "M05" }) |
We could do this, but it would make a confusing API even more confusing. The current with* family of APIs took months to hash out and the final result is very IDE-discoverable and makes it really obvious to users why you can't put a calendar or a time zone in your The exception approach would move this discoverability to runtime and would generate data-driven exceptions that we try to avoid Temporal-wide. So I would not be inclined to support this change. |
I think it's worth re-reading the arguments put forward in #592, where the current string syntax was proposed and discussed. It's also worth remembering this comment from @justingrant:
(Yes, I'm biased as the one who proposed the shortcut string arguments, but AFAIK Justin's comments still stand.) |
I agree with @gilmoreorless, especially with regards to this pattern I think we could, if we need to, live without |
Based on today's discussion I agree that we shouldn't allow However, I'm not convinced that @ptomato pointed out that there is a potential calendar conflict, but the calendar conflict already exists in This would allow us to remove the following functions:
|
Meeting 2024-05-23: At the upcoming June 2024 TC39 plenary meeting, we'll propose removing We think that this is OK for the following reasons:
I'll open a new issue to discuss |
Removal of withPlainDate is implemented here. |
PlainDate, PlainDateTime, and ZonedDateTime have with() functions as well as
withPlainTime
,withPlainDate
,withCalendar
, andwithTiimeZone
functions, each expecting a different kind of input. Should we consider only havingwith
?This issue is adapted from suggestions made by @FrankYFTang to help reduce the size of Temporal to address concerns from implementers (especially Android and Apple Watch) that Temporal's install size and/or in-memory size is too large.
There are three cases to consider:
withCalendar
andwithTimeZone
are required to ensure that changes to date/time units don't happen at the same time as the time zone or calendar. Without these methods, calls like.with({ timeZone: 'America/Los_Angeles', hour: 10})
or.with({ calendar: 'chinese', day: 5})
would be very confusing for callers because it's not clear if the result should be 10AM in the old time zone or the new time zone, or the 5th day of the month in the old calendar or the new calendar.withPlainTime
is an ergonomic support for a very common use case, which is to set the time of a PDT or ZDT instance. For example, a store that wants to consider the time of sales before the official opening time to have happened at that opening time:const notBefore8 = zdt.hour < 8 ? wdt.withPlainTime('08:00') : zdt;
. Having a single-argument method also allows a string to be used for greater convenience. The alternative towithPlainTime('08:00')
is painfully verbose:with({hour: 8, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0})
. The verbose form is also error-prone, because it's easy to forget one of those five zero-valued fields and end up with a not-zeroed result. So it seems unlikely that we'd want to remove this method.withPlainDate
, however, is less commonly used and was mostly added for consistency with withPlainTime. It seems reasonable to consider removing it to save two methods across the API.The text was updated successfully, but these errors were encountered: