TO_SYMBOL
Well-known symbol for the lazy-symbol-creation protocol used by external
library descriptors. Objects implementing [TO_SYMBOL]() are recognized as
LibrarySymbolReference values that can be passed anywhere a refkey
is accepted.
TO_SYMBOL: unique symbolRemarks
Section titled “Remarks”Implement [TO_SYMBOL]() on a descriptor object to register it as a
referenceable library symbol. Inside the method:
- Call Binder context accessor to get the current binder context.
- Look up (or create) the symbol for that binder in a
WeakMap<object, OutputSymbol>(use a sentinel object for the no-binder case, sinceWeakMapkeys must be objects). - On first creation, construct the symbol and register it into the
appropriate space for your library, passing
{ binder }(see [unresolved link]).
The method is called by language package code each time the descriptor is
used as a reference (e.g. inside ref()). It is NOT called by the binder
itself.
Example
Section titled “Example”const defaultKey = {};
const symbols = new WeakMap<object, MySymbol>();
const descriptor: LibrarySymbolReference = {
[REFKEYABLE]() {
return descriptor[TO_SYMBOL]().refkeys[0];
},
[TO_SYMBOL]() {
const binder = useBinder();
const key = binder ?? defaultKey;
let sym = symbols.get(key);
if (!sym) {
sym = new MySymbol("SomeType", space, { binder });
symbols.set(key, sym);
}
return sym;
},
};