A first look at records and tuples in JavaScript
2ality.com
2ality.com
The benefit to #{}/#[] is that # is not an existing identifier character (other than private fields) in JS, and also isn't an existing binary operator. Proposals to TC39 are constrained that they must as much as possible not "break" existing JS parsing of existing code. That removes a lot of possible characters because they already have valid parses. It also makes it tough to introduce new keywords beyond those ES3/ES5 set in stone as reserved, because they might be identifier names in user applications.
I'm wondering if {{}} is syntactically unambiguous. It's closer to the correct symbols at least. Or maybe there's a parallel to symbols (Ruby, Elixir, ?), and you could leverage a leading colon:
let myRecord = :{ foo: "bar" };
but a little tricky with ternaries. class Foo extends Bar {
public Foo() {
this->Bar();
}
}
business since it's going away soon.likewise, magic quotes.
The biggest example is, you don’t have to think in terms of references anymore. Again it’s hard to explain how much were forced to think about references and specific locations that data happens to live at, but that thinking is pervasive in how we write code (if you’re using a language like JS, which most popular languages are like).
Value semantics means you can stop thinking about data-as-a-location or data-as-a-reference and finally start thinking about data-as-data. If two records have the same values, they are the same. This means you don’t need to know anything about where data came from, you can construct matching data and say “are these things equal?” And they will be. This inherently reduces coupling and complexity.
function Record(obj)
let result = {};
for (let [key, value] of obj) {
switch (typeof value) { case "function": case "object": throw ...; }
result[key] = obj;
}
Object.freeze(result);
return result;
}
Or similar.Now obviously have typeof return a new type is exciting, and of questionable value, but having a "only immutable values can be in fields" guarantee could be useful.
That said, overloading # is not something I think is great, and the semantics need to be tightened up (fields in prototype chain, etc, what is the prototype of the resulting record/tuple object).
It seems like if you create two records with the same nested data, they will be equal when compared. This may sound small, but if you try out a language with real value semantics it changes the way you code.
Reading the proposal (TC39) explains all. https://github.com/tc39/proposal-record-tuple
Paraphrasing, if I understand TC39 correctly, an immutable map is a record and an immutable array is a tuple.
Has a certain elegance. Nicely done. While I'm inclined to like TC39, I'll chew on it to be sure.
And yeah, I’m all for immutability as a default, but breaking so pervasive an existing API is a lot of damage.