4 ms·
Perhaps I should have stated "const vs let", with "let" being JavaScript's modern, scoped version of "var"... but even so, I think this is just the sort of conf
by johnhenry 9y ago
Perhaps I should have stated "const vs let", with "let" being JavaScript's modern, scoped version of "var"... but even so, I think this is just the sort of confusion that the author is trying to clear up.
"let" in JS doesn't mean "mutable" just as "var" in Kotlin doesn't either. While one can use them as reminders to the programmer as to the mutability of the underlying object, there is nothing in the language that enforces this, so this is technically, syntactically incorrect.
In both languages, it might make more sense to simply label variables directly with something like `${variable name}_IMMUTABLE`, which gives a clear clue to the programmer that the variable is immutable, but doesn't imply anything that's untrue about the how the language is used.
Further, one can assign these variables using a library like immutable.js (https://facebook.github.io/immutable-js/ https://facebook.github.io/immutable-js/), or Kotlin's immutable collections (https://github.com/Kotlin/kotlinx.collections.immutable https://github.com/Kotlin/kotlinx.collections.immutable) to enforce a mechanism for mutability.
- blauditore 9y agoWhy not just Object.freeze()?
- johnhenry 9y agoFreezing an object doesn't freeze it's children, and thus it's still technically mutable. I think using deep-freeze (https://github.com/substack/deep-freeze https://github.com/substack/deep-freeze) might work.
- irishsultan 9y agoBecause that doesn't do what people want/expect, if you have a key with a value that's not frozen it will still be mutable, which indirectly mutates your object. See the example at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... about modifying internal.a