A different semantics for WeakMap#get default value

Mark S. Miller erights at google.com
Thu Jan 19 09:08:43 PST 2012


On Wed, Jan 18, 2012 at 11:37 PM, Andreas Rossberg <rossberg at google.com>wrote:

> On 19 January 2012 07:51, Mark S. Miller <erights at google.com> wrote:
> > Everyone on this thread, is there any need expressed in this thread that
> is
> > not satisfied by InfiniteMap?
>
> Looks good, except that I don't understand why you want a lazyFactory
> function instead of a simple initialValue -- if you recommend
> lazyFactory having no observable side effect, then (with no arguments)
> it will be a constant function, and you could as well use a plain
> value.
>
> On the other hand, a function would be useful if it also got the key
> as an argument, so that it can manufacture values in a key-dependent
> manner.


I think providing the key, or the key and defaultValue as Andrea suggests
is a fine idea. However, even without these, the answer to your question is
"fresh mutable state" as in

    function lazyFactory() { return Set(); }
or similarly
    function lazyFactory() { return []; }
etc

This addresses the only concrete use cases I've seen mentioned in this
thread -- using nested single-key single-valued collections to emulate
either

a) multi-valued collections, i.e., MultiMaps, as single-valued collections
from keys to sets of values, or

b) multi-keyed collections, i.e., a two dimensional map, as a map from a
first key to a collection mapping from a second key (array index) to a
value.



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


More information about the es-discuss mailing list