|
If you're mutating any object, it is necessarily a value with reference semantics, period. Objects that do not have reference semantics are immutable. Some objects that cannot be mutated (like numbers) can have reference or value semantics depending on how they are implemented. For instance, a bignum integer always has reference semantics. Small integers usually have value semantics: they fit into a machine word with no heap part. In that case, all instances of the number 0 or 1, and some range beyond that in both directions, will always be the same object according to eq. If you're mutating an object (and, thus, something that has reference semantics), the difference that the reference semantics makes is that other parts of your program may hold a reference to that object; your code has not received a copy. If you haven't accounted for that, you probably have a bug. Sure, this stuff isn't obvious; unless you already know another dynamic language like Ruby, Javascript, Python, ... Complete neophytes have to be taught it from the fundamentals. |