WeakPair primitive ?

2011-09-15 Thread Sean Eagan
Would a WeakPair primitive be useful...

let wp = new WeakPair(key, value);
...
if(wp.hasKey(key)) {
  foo(wp.value());
}

WeakMap could be built on top of this primitive:

class WeakMap {
  constructor() {
private pairs = new Set;
private getPair = function(key) {
  for(i of private(this).pairs.iterator()) {
if(i.hasKey(key)) return i;
  }
  return undefined;
}
  }
  get(key) {
let pair = private(this).getPair(key);
return pair ? pair.value() : undefined;
  }
  has(key) {
return !!private(this).getPair(key);
  }
  set(key, val) {
if(!isObject(key)) throw new Error;
let pair = private(this).getPair(key);
let pairs = private(this).pairs;
if(pair) {
  pairs.delete(pair);
}
pairs.add(new WeakPair(key, value));
  }
  delete(key) {
let pair = private(this).getPair(key);
if(pair) {
  let pairs = private(this).pairs;
  pairs.delete(pair);
}
  }
}

Are there other useful abstractions that could be built on top of
WeakPair, beyond WeakMap ?

Thanks,
Sean Eagan
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: WeakPair primitive ?

2011-09-15 Thread Allen Wirfs-Brock

On Sep 15, 2011, at 10:21 AM, Sean Eagan wrote:

 Would a WeakPair primitive be useful...
 
 let wp = new WeakPair(key, value);
 ...
 if(wp.hasKey(key)) {
  foo(wp.value());
 }
 
 WeakMap could be built on top of this primitive:

It's not that easy.  Read 
http://www.arnetminer.org/dev.do?m=downloadpdfurl=http://arnetminer.org/pdf/PDFFiles/--g---g-Index1247931776950/Ephemerons
 A New Finalization Mechanism1247944577286.pdf

You need Ephemeron pairs to build such abstractions

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: WeakPair primitive ?

2011-09-15 Thread Rick Waldron
WeakMap is in Harmony...
http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps

On Thu, Sep 15, 2011 at 1:47 PM, Allen Wirfs-Brock al...@wirfs-brock.comwrote:


 On Sep 15, 2011, at 10:21 AM, Sean Eagan wrote:

 Would a WeakPair primitive be useful...

 let wp = new WeakPair(key, value);
 ...
 if(wp.hasKey(key)) {
  foo(wp.value());
 }

 WeakMap could be built on top of this primitive:


 It's not that easy.  Read 
 http://www.arnetminer.org/dev.do?m=downloadpdfurl=http://arnetminer.org/pdf/PDFFiles/--g---g-Index1247931776950/Ephemerons
 A New Finalization Mechanism1247944577286.pdf

 You need Ephemeron pairs to build such abstractions


 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss


___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss