-
Notifications
You must be signed in to change notification settings - Fork 154
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 parameterless conversion methods that return objects (e.g. DateTime.prototype.toDate
) be property getters instead?
#724
Comments
For any of the ones that produce a new object every time, absolutely not - that's just massively confusing. Are there any that take no arguments and return the same |
Yep. The
If reference equality is the main concern, could object-returning properties be backed by slots that are lazily initialized on first call of the getter? |
That approach is one that members of the committee explicitly rejected for AggregateError's the |
Less typing. I'm particularly thinking of legacy
I read tc39/proposal-promise-any#38 but I'm not sure I fully understand the scope of the issue. What's a good summary of the problem that's being avoided by not having shared object state? Would it be OK for a getter to return a frozen instance of a built-in type like |
Code is read much more often than it's written, and avoiding the Yes, the only safe way to return an object from a prototype getter would be if it's deeply frozen - ie, if there's no way to attach information to it that someone else calling the same getter can receive. |
Is this an argument against property getter functions in general? What are cases where getters are OK? I admit that I don't see the confusion-- if property values are deterministic based on immutable internal slots and have no side effects, then I'm not sure why a shorter-parentheses-less syntax is less clear. But maybe I'm missing something important about why? Also, there are many property getters in Temporal, including both calculated props like
@ptomato - How is this issue handled with the |
It's not; the issue was raised in the June TC39 meeting but the person who raised it wasn't personally there, and I don't remember there being a consensus (yet) that it is a constraint that the JS language must adhere to everywhere. I'm attending a call about it soon, in order to learn more. |
Sure. The call is tentatively scheduled for next week: https://github.com/tc39/incubator-agendas/blob/master/2020/07-14.md |
I just opened #750 to track the question of the getters in |
toFoo
& getFoo
methods be property getters instead?toFoo
& getFoo
object-returning conversion methods be property getters instead?
toFoo
& getFoo
object-returning conversion methods be property getters instead?DateTime.prototype.toDate
) be property getters instead?
FWIW, my feeling is that the type conversion methods should be methods, not property getters. More out of a conceptual understanding of what those methods do, rather than technical reasons. |
We've decided (in 2020-08-13 meeting) to leave the status quo in place, with the following heuristic:
Closing as resolved. |
Temporal has many "downscaling" conversion methods that remove data so accept no properties, e.g.
DateTime
-toDate
,toTime
,toYearMonth
,toMonthDay
(after Rename conversion methods to toFoo #705)Date
-toYearMonth
,toMonthDay
(after Rename conversion methods to toFoo #705)LocalDateTime
-toDateTime
,toDate
,toTime
,toYearMonth
,toMonthDay
Should these be property getters (
.date
,.time
,.yearMonth
,.monthDay
) instead? Some reasons we might want to do this:Time.prototype.toDateTime
) from conversion methods that don't need any more data (e.g.DateTime.prototype.date
).from
orwith
call ? #720 is adopted (this issue is essentially the inverse of Easier way to combine multiple Temporal objects in onefrom
orwith
call ? #720, although each could be done independently)now
methods. If you learn how to usenow
, you can apply that knowledge to conversions.LocalDateTime
, provides more consistency between access to.calendar
,.absolute
, and.timeZone
properties (which will be property getters) with access to.date
,.time
,.yearMonth
,.monthDay
which are currently methods..calendar
with access to.date
,.time
,.yearMonth
,.monthDay
.Note: #750 discusses whether
Absolute
getter methods likegetEpochNanoseconds
(which return primitives, not objects) should be changed to properties likeepochNanoseconds
. This topic was originally part of this issue but I moved it over to #750 so that we could focus this issue on the challenges of object-valued property getters.The text was updated successfully, but these errors were encountered: