proposal-record-tuple icon indicating copy to clipboard operation
proposal-record-tuple copied to clipboard

Record.keys/entries/values

Open bakkot opened this issue 2 years ago • 4 comments

I just noticed that the Record constructor does not have a way of getting the keys out of a Record. In fact, as far as I can tell, there is no such facility anywhere in this proposal.

An earlier version had a Record.keys method which was like Object.keys except it returned tuples. I agree that such a thing need not exist in V1 of this proposal. But I was actually expecting Record.keys to be like Object.keys except that it accepted Records. Was that discussed and rejected? If it was, is there an alternative way of getting the keys from a Record (short of a for-in loop)? It seems like there ought to be such a thing, and it seems like the obvious place to put it would be Record.keys.

Ditto, of course, for values and entries.

bakkot avatar Jul 25 '22 17:07 bakkot

Earlier discussion here: #133

Closed as "not part of V1". Object.keys will work with records, in the same way that it works on strings.

acutmore avatar Jul 25 '22 17:07 acutmore

#133 is talking about a different feature than what I am discussing here; see my second paragraph.

I would find it pretty strange to pass a Record to a method on the Object constructor, but I guess it's better than such a facility not existing at all. (I also find it very strange to pass a string to the Object constructor; I've never done that and would look askance at any code which did.)

bakkot avatar Jul 25 '22 17:07 bakkot

(I also find it very strange to pass a string to the Object constructor; I've never done that and would look askance at any code which did.)

I agree, I only meant to demonstrate that Object.keys already works with primitives so without changing anything it already works for records. The part I would find strange about passing a string is not that it’s a primitive and more that it’s a list, I would also find it odd to pass an array to Object.keys. To me, Records being bags of string properties makes it feels more natural to ask for it’s object’s keys.

acutmore avatar Jul 26 '22 07:07 acutmore

I do think it’d be perfectly fine to add keys/values/entries/fromEntries to Record, fwiw, I’m just not sure the use case is compelling enough. (also probably hasOwn for consistency even tho it can’t have any inherited properties)

ljharb avatar Jul 26 '22 08:07 ljharb