-
Notifications
You must be signed in to change notification settings - Fork 3
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
Introduce AbstractMap
and AbstractSet
for real.
#6
Comments
It's not a good idea. It will break too much code. Some code relies on existent collections prototype chain, some code check existence of own methods on collections prototypes (for example, this check works for all methods in For consistency with these conceptions, we should have We already have |
Thanks for the counter-example! It looks valid. @ljharb , do you find this a convincing counter-example? Should we return to our original plan where the |
My personal preference is that we proceed with the surgery that @ljharb proposed. It would make for a much cleaner system. But if that isn't practical, I'm perfectly happy to retreat to our original position. Re #5 , if we retreat, then we'd duplicate the new query methods in the same places we duplicate the old query methods: |
That example is for DataView; @zloirock, do you use the same pattern with Map and Set? I don't see it, looking through the code. @erights this is a convincing example of the pattern - but we'd need concrete examples of the pattern being used for Map, Set, WeakMap, and/or WeakSet, otherwise I think it's worth the effort. |
So you really think that adding those abstractions only to those collections when this proposal is much wider is a good idea?
@ljharb I wrote that |
The plan here is to duplicate all the methods so that Map.prototype's own properties would not change, only its prototype chain. Yes, improving anything is a good idea even if everything can't be improved. |
However, this is not an improvement, it's a complication. See my first comment, mainly the last paragraph. |
I don't agree with that comment. Map and Set should have a common superclass, as should WeakMap and WeakSet. It's fine if the weak and non-weak collections aren't part of the same hierarchy. |
But, even if you don't agree, it will break the web. |
That's what I'm looking for concrete evidence for; your DataView example isn't that. Can you show some? (to be clear - code that will break if Map.prototype or Set.prototype does not inherit from Object.prototype) |
|
BTW why |
and I wrote that |
So, in this case, why add an unnecessary possible case of breakage which anyway can't be implemented for the rest collections like |
Just because we can't fix everything doesn't mean we shouldn't try to fix anything. DataView and Array aren't the same kind of collection as Map and Set - the latter are the more important ones to improve. (There's a bunch of proposals to add methods to Map and Set and Array, and none to DataView, for example - and that's been totally fine so far) |
I disagree.
At least:
|
Those are for specific types; they're not general collection methods for any type. |
This does not matter. |
I agree that we need uniformity across all collection that this proposal applies to. I agree that such uniformity is more important than the benefits of introducing I like the point about how |
I also agree. Inheritance seems like the wrong tool for the job. Right now, there's a clean hierarchy we can construct to show the relationship between Map, ReadOnlyMap, and FixedMap, but there's no guarantee it'll stay that way. I know this is contrived, but what if, one day we decide to add a write-only view, where you can only add new values to a map, but you can read what's currently there? How would that fit into the hierarchy? The issue is that FixedMap and ReadOnlyMap are defined as a subset of Map's functionality, but there's other subsets of functionality we could create from Map. Inheritance is the wrong tool for this job. What we're really wanting here is an interface - a feature Javascript does not have, and something that should not be emulated with inheritance. Instead, as @erights mentioned, we should just document this fictional interface and let users rely on duck-typing. If something more robust is wanted, a user can use typescript, where they can have a real interface to model the relationship between Map, FixedMap, and ReadOnlyMap. |
@zloirock ... what would an |
@SamB for example, expose common methods / accessors for |
During the tc39 post-presentation discussion, it was suggested that
AbstractMap
andAbstractSet
be introduced as actually "superclasses", not just spec fictions, aboveMap
andSet
as well asReadOnlyMap
andReadOnlySet
. IOW,Map.prototype
andReadOnlyMap.prototype
would both inherit directly fromAbstractMap.prototype
.FixedMap.prototype
would continue to inherit directly fromReadOnlyView.prototype
, and therefore indirectly fromAbstractMap.prototype. All the map query methods would be on
AbstractMap.prototype. Neither
ReadOnlyMap.prototypenor
FixedMap.prototypewould add any new methods to those they inherit.
Map.prototype` would add the update operations.I had avoided this in the initial proposal because this is the one change that would potentially break old code. However, @ljharb suggested that, if we also left the query methods on
Map.prototype
itself as well as onAbstractMap.prototype
, then the only observable difference from old code is the splicing ofAbstractMap.prototype
in the inheritance chain betweenMap.prototype
andObject.prototype
. If this doesn't break too much old code, it is indeed attractive.Likewise of course for the other existing collections.
The text was updated successfully, but these errors were encountered: