A different semantics for WeakMap#get default value

Axel Rauschmayer axel at rauschma.de
Mon Jan 16 13:26:48 PST 2012


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



More information about the es-discuss mailing list