WeakPair primitive ?
# Allen Wirfs-Brock (14 years ago)
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 www.arnetminer.org/dev.do?m=downloadpdf&url=http://arnetminer.org/pdf/PDFFiles/--g---g-Index1247931776950/Ephemerons A New Finalization Mechanism1247944577286.pdf
You need Ephemeron pairs to build such abstractions
# Rick Waldron (14 years ago)
WeakMap is in Harmony... harmony:weak_maps
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