{Weak|}{Map|Set}

Sean Eagan seaneagan1 at gmail.com
Thu Sep 15 08:41:08 PDT 2011


Why constrain WeakMap to object keys, but not Map and Set ?  I think
it is because "Weak" only makes sense for object keys.  However, if we
base the distinction on iterability rather than weakness as David
suggested, then we do not have to include an object key constraint
anywhere.  We could have:

Map - current WeakMap minus object key constraint
IterableMap - current Map
Set - a "WeakSet" (David's above use case) minus object key constraint
IterableSet - current Set

This would also encourage use of the non-leaky abstractions where
iterability is not a requirement.

Also, what are the iteration order semantics ?  Consider the following:

let s = new Set // i.e. IterableSet
s.add(0);
s.add(1);
// clearly [0,1] here
s.add(0);
// [1, 0] now ?
s.delete(0);
s.add(0);
// clearly [1, 0] here

let m = new Map; // i.e. IterableMap
m.set(0, 2);
m.set(1, 2);
// clearly [0,1] here
m.set(0, 2); // set to existing value
// [1, 0] now ?
m.set(0, 3); // actually change value
// [1, 0] now ?
m.delete(0);
m.set(0, 2);
// clearly [1, 0] here

Also, for the iteration API of Map (i.e. IterableMap) and Set (i.e.
IterableSet), why not integrate with iterators [1] :

let m = new Map, s = new Set;
...
for(i of m.iterator()) {
 ...
}
for(i of s.iterator()) {
 ...
}

[1] http://wiki.ecmascript.org/doku.php?id=harmony:iterators

On Thu, Sep 15, 2011 at 2:10 AM, Brendan Eich <brendan at mozilla.com> wrote:
> On Sep 14, 2011, at 11:09 PM, Allen Wirfs-Brock wrote:
>
>> I would prefer ObjectMap (the keys are restricted to objects).
>
> Now that you point it out (again), I agree.
>
> People who know a bit about GC also confuse WeakMap with WeakRef/WeakPtr, which we have only in the strawman space:
>
> http://wiki.ecmascript.org/doku.php?id=strawman:weak_references
>
> /be
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>

Thanks,
Sean Eagan


More information about the es-discuss mailing list