3 ms·
So from reading this it sounds like what's being asked for essentially syntactic sugar for Object.freeze(), and the function Record(obj) would be doing somethin
by olliej 6y ago
So from reading this it sounds like what's being asked for essentially syntactic sugar for Object.freeze(), and the function Record(obj) would be doing something along the lines of
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).
- amw-zero 6y agoYou only looked at the conversion of an Object to a Record. That’s really not the point of this. The point of introducing true records and tuples is to bring value semantics to JS for compound data types. 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.