A different semantics for WeakMap#get default value

David Bruant bruant.d at gmail.com
Mon Jan 16 13:56:12 PST 2012


Le 16/01/2012 21:56, Mark S. Miller a écrit :
> I like the idea of the two argument get as proposed by David. (In
> fact, I like it so much that I thought I'd already specified it that
> way ;).) David, 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>.
>
>
> Axel, I do not like your elaboration because it turns a query
> operation into a possibly occasionally side-effecting operation. As it
> is now, I can give someone a read-only view of a WeakMap wm my simply
> giving them 
>
>     { get: wm.get.bind(wm), has: wm.has.bind(wm) }
This applies to my proposal as well.
Bind can allow currying the arguments. Unfortunately, currying here
would happen in the wrong order (currying the key, not the default value).

wm.get.bindReverseCurry(2, wm, undefined) would be what you want (sorry
for the bad name)

f.bindReverseCurry(end, thisVal, ...args) returns g such as:
g(...gargs)
<=>
f.apply(thisVal, gargs.slice(0, end - argsToRevCurry.length).concat(args) )
(I'll deal with end < argsToRevCurry.length another day :-) )

-----
var slice = Function.prototype.call.bind(Array.prototype.slice);

Function.prototype.bindReverseCurry = function(end, thisVal){
    var toCall = this;
    var argsToRevCurry = slice(arguments, 1);

    return function(){
        var args = slice(arguments, 0, end - argsToRevCurry.length);
        return toCall.apply(thisVal, args.concat(argsToRevCurry));
    };
}

// test:
parseInt2 = parseInt.bindReverseCurry(2, undefined, 2);
console.log(parseInt2("101")); // 5
-----

Would such a solution sounds satisfactory to provide a read-only view of
a weakmap?

David


More information about the es-discuss mailing list