by capableweb · 4y ago · 0 comments
const is JS is referring to the reference and has nothing to do with the value, it's not even shallow immutability, there is none at all.
const arr = []
arr.push(1)
is valid JavaScript.Bit nitpicky maybe, but wouldn't want people to get the impression const gives you any sort of immutability.
const makes the binding immutable but not the object. Object.freeze makes the object immutable but not it’s children.
The same argument doesn't apply to Data's shallow immutability. It will give you errors when mutating at least some of the fields. If your code can catch you mutating a number, then you can notice the bug and be reminded to make deep copies etc. It's an improvement, just like Object.freeze.
To take Python tuples (which are morally pretty close to Data)
my_data = ([],false)
my_data[0].push(1)
tuples are immutable, but this happens, because the "immutability" is in the same vein as JS's const: "we are always pointing to the same object".The only conceptual difference between this and JS's `const` is that you can't use `const` to declare object properties immutable; for that you need Object.freeze().