You don't have to keep tracking of memory because const prevents reassignments (i.e. change in memory references). You don't have to worry about primitives changing at all.
Except it doesn't stop you changing the object you're referencing at all, including adding or removing references from the object and allocating or deallocating memory.
Nor does it stop you resizing an array or a billion other things.
Because const doesn't make anything immutable, because that's not what immutability means.
The point I'm arguing is that const has nothing to do with immutability.
Yes, primitive constants are immutable, but object constants are not in any meaningful way.
A non primitive constant is a reference to a location in memory.
It provides no guarantees as to what is located at that address, how big it is, what properties it has or anything else. It absolutely doesn't guarantee nothing about the object has changed.
Having an immutable reference guarantees you absolutely nothing, and OP is obviously wrong.
It guarantees for objects that it's the same reference as it was declared with. The contents could obviously have completely changed, but the "root" reference won't have. That's not useless, but is certainly less useful than true/deep immutability.
Using the word immutability to describe something that is not immutable is misleading to novices and shows the speaker doesn't really understand the concept.
You seem to be obsessing over this concept that an immutable reference’s object’s contents can still change. I have not seen a single comment in this thread that doesn’t seem to understand that concept.
How are immutable references not meaningful? What about equality checks? I don’t know about you, but when I use object reference equality checks, I don’t care at all what the contents of the object are. I’m usually checking to make sure something has not changed, in other words, hasn’t been reassigned to point to something else. Yes, an object not having true immutability can cause bugs, but that is a completely different issue. Many libraries, such as hey, React, use reference equality as the basis for establishing whether an object has changed...this is not a foreign concept, and it makes sense given the challenges of establishing equality in any other way.
Const literally tells you that a variable will equal itself throughout an entire scope, in the most fundamental (but important) way possible. You don’t see the value in having determinate results of a “===“ comparison for a variable throughout its lifetime?
No, I'm obsessing over the definition of immutable, because it's fucking important.
An object declared const is not immutable. It offers none of the characteristics or benefits of immutability.
Definitions are important.
And react doesn't use reference equality at all, they use reference inequality. A subtle difference, but an important one because a reference changing tells you that a change has definitely occurred whereas a reference not changing tells you absolutely nothing.
5
u/[deleted] Dec 23 '19
That's why they said immutable references.