A different semantics for WeakMap#get default value

Brendan Eich brendan at mozilla.org
Mon Jan 16 13:54:20 PST 2012


Mark objected to the mutating-option-for-"get" overloading. I've agreed 
but wanted to go further here: optional trailing boolean parameters are 
an anti-pattern. I'm leaning toward thinking that all boolean option 
params are an anti-pattern.

/be

> Axel Rauschmayer <mailto:axel at rauschma.de>
> January 16, 2012 12:26 PM
> One probably should separate two concerns: providing a default value 
> vs. adding missing values to the collection.
>
> One possible signature:
>
>      function get(key, default = undefined, addIfMissing = false)
>
> The above would also allow you to store `undefined` and detect missing 
> values, by using a special value (e.g. named NO_VALUE) as the default.
>
>
>
> -- 
> Dr. Axel Rauschmayer
> axel at rauschma.de <mailto:axel at rauschma.de>
>
> home: rauschma.de <http://rauschma.de>
> twitter: twitter.com/rauschma <http://twitter.com/rauschma>
> blog: 2ality.com <http://2ality.com>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> David Bruant <mailto:bruant.d at gmail.com>
> January 16, 2012 12:09 PM
> Hi,
>
> I recently wrote some code using WeakMaps and the following pattern
> keeps coming over and over:
> -----
> var v = wm.get(o);
> if(v === undefined){
> v = someValue;
> order.set(o, v);
> }
>
> // now, wn has a value for o (either it already did or it's been added)
> // v === wm.get(o)
> // play with v
> -----
>
> The "v === undefined" is fine in my case, because I know I never store
> "undefined" as a value.
>
> I thought that maybe the semantics of WeakMap#get could be changed
> whenever the key is not used to set the value and return it.
> My pattern would be reduced to:
> -----
> var v = wm.get(o, someValue);
> // now, wn has a value for o (either it already did or it's been added)
> // v === wm.get(o)
> // play with v
> -----
>
> Thinking more about the current semantics, I thought that what is
> currently done could be achieved a bit differently:
> -----
> var v = wm.get(o, def);
> // almost equivalent to:
> var v = wm.get(o) || def;
> -----
> There is a difference when the vaue stored in the weak map is a falsy 
> value.
>
> My personal experience is to store mostly objects (so truthy) as weak
> map values, so I wouldn't be affected. Has anyone else experience in
> storing falsy values?
>
> David
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120116/4d66d90d/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: postbox-contact.jpg
Type: image/jpeg
Size: 1222 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120116/4d66d90d/attachment-0002.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compose-unknown-contact.jpg
Type: image/jpeg
Size: 770 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120116/4d66d90d/attachment-0003.jpg>


More information about the es-discuss mailing list