Skip to content

Instantly share code, notes, and snippets.

@joliss
Last active September 14, 2015 19:04
Show Gist options
  • Select an option

  • Save joliss/7ca89af5947ddf7c9f57 to your computer and use it in GitHub Desktop.

Select an option

Save joliss/7ca89af5947ddf7c9f57 to your computer and use it in GitHub Desktop.

Re https://github.com/nikomatsakis/typed-objects-explainer/blob/master/valuetypes.md (recent TC39 notes):

I wonder if the immutability requirement can be dropped. To make this work, assignment of value types (color2 = color1) would need to always copy, never be by-reference.

In C++ for example, the complex number class has value semantics (assignment creates a copy, equality compares its constituent values), and yet you can mutate it with c.real(5.0) (sets real part). Similar for vector, which can be mutated with v.push_back(10). This is really no different than int having value semantics but also being mutable (++n). I'm a big fan of C++'s semantics, as they allow you to construct complex objects by repeatedly mutating them and still get value semantics.

So in JavaScript, we'd be able to do this:

var color = ColorType({r: 22, g: 44, b: 66, a: 88});
var color2 = color; // this had better be a copy!
color.b = 77; // works
color.b // => 77
color2.b // => 66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment