A different semantics for WeakMap#get default value

Mark S. Miller erights at google.com
Mon Jan 16 13:46:13 PST 2012


David and Axel, I don't get it. What's wrong with the get with the optional
default value, as specified by that text and as currently implemented by
FF? What additional value does this contingent mutation add, regardless of
how it's packaged? Do you really want "has" to answer true for a key merely
because a previous get (or setdefault) with that key caused a default value
to be stored? Axel, does this add enough value to expand the API by 25%?

On Mon, Jan 16, 2012 at 1:26 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> A separate method such as the previously mentioned setdefault() provided
> by Python [1] might answer both Mark’s concerns and yours.
>
> [1] http://docs.python.org/dev/library/stdtypes.html#mapping-types-dict
>
>
> On Jan 16, 2012, at 22:21 , David Bruant wrote:
>
> > [Specifying the Davids]
> >
> > Le 16/01/2012 21:56, Mark S. Miller a écrit :
> >> I like the idea of the two argument get as proposed by David [BRUANT].
> >> (In fact, I like it so much that I thought I'd already specified it
> >> that way ;).) David [HERMAN], I'm glad to see you got it right in your
> >> suggestion at
> >> <
> http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#alternate_spec_based_on_ff6.0a1
> >.
> > Actually, that's not what I meant.
> > What I meant was to replace the get that is currently where you link to
> with
> > ------
> > get(key, defaultValue = undefined) {
> >      if (key !== Object(key)) {
> >        throw new TypeError(...); // key must be an object
> >      }
> >      const i = private(this).keys.indexOf(key);
> >
> >      if(i < 0 && defaultValue !== undefined){
> >        this.set(key, defaultValue);
> >      }
> >
> >      return private(this).values[i];
> > }
> > ------
> >
> > In this version, if the key is not found, the property is set to the
> > return value.
> > The return statement returns the value that is there if the key existed
> > in the map or the default value (which has just been assigned).
> >
> > David
> >
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120116/99c06816/attachment-0001.html>


More information about the es-discuss mailing list