-
Notifications
You must be signed in to change notification settings - Fork 17
Conversation
Co-authored-by: Justin Ridgewell <justin@ridgewell.name> Co-authored-by: Jordan Harband <ljharb@gmail.com>
In my vision, static methods look like a good option, but in addition to
|
Array.from is a static method that accepts an iterable or arraylike; that’s the precedent we should follow. |
|
All |
dc1847e
to
04cc68f
Compare
I think it'll be xor. We'll either decide that we like the static methods, and only have them, or we'll decide we don't and go back to picking a new name for the prototype methods.
I copied |
I am ok with the Map version being generic (though I personally wouldn't bother), but the |
|
Also, for the Map fallback, I think it would be more natural to create an array of pairs and invoke the constructor with that, rather than invoking I don't feel strongly about how the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel pretty strongly that the Object one should accept arraylikes, not just iterables (altho it's fine for Map to just accept iterables)
I definitely agree with @bakkot's comment; it's better to make an Array and construct a Map from it rather than observably calling |
Unfortunately my suggestion will still observably call Now that I think about it, it will in addition look up the array iterator and invoke that. My suggestion has strictly more observable calls, at least for the default Map constructor. ... this is definitely inclining me towards not trying to support subclassing, and instead just constructing a |
Strong disagree. Note that Object.fromEntries doesn't accept arraylikes. |
Rereading tc39/proposal-object-from-entries#9 and tc39/proposal-setmap-offrom#3, I'm not sure why I didn't push harder for it to do so, because there's not really a compelling argument there that i see. |
I feel the same way as tc39/proposal-setmap-offrom#3.
I think this falls under Type II, which we decided is useful? |
That's not what the proposal says, though of course getting rid of Type II in existing method will be fraught and I don't want to reopen that discussion. But FWIW, for Set methods we decided that there will be no receiver-sensitivity. Incidentally see some discussion here and on the next few days. Sentiment seemed to be against relying on the receiver for statics. |
1dafecf
to
45377b2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Removed
Map
subclass support.
Other than my opinion on arraylikes, this looks great to me. |
FWIW, I feel slightly uncomfortable about removing subclassing, especially since these methods are static.
IIUC, generic instance methods are icky because
If a subclass (a) makes |
The Promise behavior for statics (throwing if invoked without a receiver) is pretty annoying, since it makes it harder to use on the base class, not just on subclasses. Subclassing is rare enough that I am hesitant to make the base class case worse for the benefit of subclasses. We could match Array with the fallback, but I don't really see the benefit - a subclass would need to override it anyway so that |
Promise subclassing could be rare; Map/Set are probably not, since things like
The fallback is just a best attempt at being useful, not that subclasses always produce consistent results as the base class. This example looks terribly like auto-bound methods; if you want to go down that path, you would probably use a getter like People are always going to be surprised by receivers somewhere, and fixing receiver issues these days is relatively cheap (either arrow function wrapper or What surprises me is that basically nothing works with subclassing these days; in order to make a subclass with a different primitive method (the minimal-core thing) you have to reimplement everything. That sounds like a leak of abstraction. |
Subclassing itself isn't a well-defined concept in the language - TC39 hasn't even been able to come to consensus about what the officially supported subclassing mechanism should be - and it's just not a thing that's commonly done in the ecosystem. I'd suspect your 2% and 10% is more like 0.01% and 80%, personally. |
Having a method which behaves inconsistently depending on how you call it is a large cost. It's not something to be glossed over lightly.
We do not want to go down that path. Making statics receiver-insensitive works just as well, except for subclasses, and there's no way to make subclasses work with this without auto-bound methods, which we're not going to do.
I would put it a different way: everything works with subclassing. It's just not offering hooks for subclasses to customize the behavior of base classes. If you want to change the behavior of a method, you need to offer your own implementation of the method. That's not an abstraction leak - that's just what subclassing is. |
Note that |
A downside of this approach is that half of the users will think that the order of the arguments is I don't think we have precedent for data-first or data-last yet. The closest I can think of is with the mapper argument to |
Would you consider |
|
I'm strictly for |
For what it's worth, I implicitly assumed that the array-like input would be first, since these are being converted from Array.prototype methods, and my background in Python leads me to think of the method receiver (in languages where that's a thing) as a possibly-hidden first argument, which in JavaScript just happens to always be hidden. |
I've been thinking 🤔 lately, that maybe we can simply rename methods to:
And ship 🚀 it asap, rather then trying figure out arguments? 😀 |
5d5b155
to
951513d
Compare
This PR has been rebased. Spec can be viewed here: https://raw.githack.com/tc39/proposal-array-grouping/static-method/index.html |
This reimplements the proposal using static methods on the
Object
andMap
constructors. There seemed to be positive support for doing so in today's plenary, allowing us to side-step web compat issues that crop up from developers using arrays as hashmaps with arbitrary key-values.The factories are made to be generic, which means that we may not return an prototype-less object fromObject.groupBy
(eg, viaclass Foo extends Object {}; Foo.groupBy(…)
), and we use themap.set
method instead of directly inserting into the[[MapData]]
.This has not yet reached consensus, this is just a preview for us to discuss further at the next meeting.