Symbols as Weak Keys
for Stage 3
Leo Balter
Primary Goals of this proposal
Footer
What's new?
var unique = Symbol();
var wm = new WeakMap();
{
const obj = {};
wm.set(unique, obj);
}
wm.has(unique); // true
Footer
Support for other "Weak" APIs
This proposal also extends functionality to allow Symbols in WeakSets, WeakRefs and FinalizationRegistry, matching consistency with WeakMap keys.
Footer
WeakSet, WeakRef, and FinalizationRegistry
var unique = Symbol();
var ws = new WeakSet();
ws.add(unique);
var wr = new WeakRef(unique);
var registry = new FinalizationRegistry(heldValue => {
// ....
});
registry.register(unique, 'something');
Footer
Status
Footer
Consensus for Stage 3?
Footer
Thank You
Proposed alternative
Restrict Symbols to be truly unique.
Why?
Footer
Proposed alternative
Restrict Symbols to be truly unique.
Why not? (general arguments against this alternative)
Footer
Use Cases
This proposal itself provides better ergonomics for distinct values being used just as keys of WeakMaps.
Rather than creating custom meaningless objects, symbol values can provide a better purpose while still being primitive values.
ECMA-262 already specifies symbols as primitive values to be used as unique non-string keys.
Ergonomics
Footer
Use Cases
Symbols, dereferenced through WeakMaps, is a reasonable option as a way forward to reference Objects from Records and Tuples.
The alternative is using the Box semantics but that seems to be unresolved yet.
Records & Tuples can't contain objects, functions, or methods and will throw a TypeError when someone attempts to do it.
Records and Tuples
Footer
Use Cases
Membrane systems are used on top of Realms for communication in virtualized environments.
Membranes rely on WeakMaps with many references of values used cross realms. Using symbols as keys to capture membrane references would be a good case to meet better ergonomics.
In the current API model for Realms, objects can't be accessed cross realms. This proposal would allow a membrane system to access distinct from different realms to be directly used as WeakMap keys.
Realms and Membranes
Footer