Questioning WeakMap.prototype.clear

Rick Waldron waldron.rick at gmail.com
Mon Jan 21 12:21:56 PST 2013


On Mon, Jan 21, 2013 at 2:52 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:

>
> On Jan 21, 2013, at 11:36 AM, Rick Waldron wrote:
>
> > This is the reality check I can get behind—I'm hard pressed to come up
> with a use case that isn't contrived or solvable by some other means.
> >
> This is easy:
>
> I'm do phased traversals over a complex data structure.  I have a number
> of functions that collaborative perform the function and they share access
> to a WeakMap to cache relationships that they identify over the course of
> the traversal.  When I start a new traversal phase I want to flush the
> cache so I use the clear method to do so.
>

This use case is similar to the cases I was also thinking of; when I wrote
my response, I had just read through the thread and felt that the user-land
implementation, suggested by Andrea and Mark, was sufficient as "solvable
by some other means". The impl I'm referring to:

class WeakMapWithClear {
  private let wrapped; // not const, however we say this for a field
  constructor() {
    wrapped = new WeakMap();
  }
  get(key) => wrapped.get(key),
  set(key, val) => wrapped.set(key, value),
  has(key) => wrapped.has(key),
  delete(key) => wrapped.delete(key),
  clear() => { wrapped = new WeakMap(); }
}

Subjectively, I don't like this at all and don't think it's fair to put the
burden on user-code.

Rick



> Allen
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130121/42496abc/attachment.html>


More information about the es-discuss mailing list