Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Box from the proposal #277

Merged
merged 8 commits into from
Dec 17, 2021
Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Dec 17, 2021

After the last TC39 meeting, it's clear that we won't be able to move forward with object placeholders (aka boxes). They will be left to userland implementations that rely on the Symbols as WeakMap keys proposal.

An implementation would look like this:

export { Box as default };

const symToObj = new WeakMap(); // use a Map() until symbols as weakmap keys is supported in engines
const objToSym = new WeakMap();

const Box = (value) => {
  if (objToSym.has(value)) return objToSym.get(value);
  const sym = Symbol();
  symToObj.set(sym, value);
  objToSym.set(value, sym);
  return sym;
};

Box.unbox = (sym) => {
  if (!symToObj.has(sym)) throw new TypeError();
  return symToObj.get(syn);
};

Box.isBox = sym => symToObj.has(sym);

and it can be used like

import Box from "./box.js";

let myObj = { x: 1 };

let rec = #{ box: Box(myObj) };
Box.unbox(rec.box) === myObj; // true

It's possible that after seeing how records&tuples will be used in the wild (and if the community will converge on some specific "Box" patterns) we will be able to present it as a follow-on proposal, but for now boxes/placeholders are keeping the proposal stuck at stage 2 indefinitely.

Closes #270, closes #240, closes #257

@Jamesernator
Copy link

Does symbols as weakmaps keys have likely consensus to move forward? It would be unfortunate it records/tuples shipped but that proposal failed to move forward.

@ljharb
Copy link
Member

ljharb commented Jan 8, 2022

It wouldn’t be possible for that to happen; symbols as weakmap keys would need to ship first.

@Jamesernator
Copy link

It wouldn’t be possible for that to happen; symbols as weakmap keys would need to ship first.

Well fully immutable records and tuples could be perfectly functional without the other proposal (e.g. things like #{ x: 10, y: 20 }), they just wouldn't be able to reference objects in a non memory-leaky way.

@iwikal
Copy link

iwikal commented Jan 9, 2022

It wouldn’t be possible for that to happen; symbols as weakmap keys would need to ship first.

There seems to be disagreement on that point. From the current readme of the Symbols as WeakMap Keys proposal:

[...] Record and Tuple is viable and useful without additional language support for boxing objects. This proposal attempts to describe solutions that complement the usage of these userland solutions with Record and Tuple, but is not a prerequisite to landing Record and Tuple in the language.

https://github.com/tc39/proposal-symbols-as-weakmap-keys/blob/5f9501b5570ff472a43b03fc42e87d1e89ded213/README.md#record-and-tuples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants