Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an immutable type change its internal state?

The question is simple. Can a type that can change its internal state without it being observable from the outside be considered immutable?

Simplified example:

public struct Matrix {     bool determinantEvaluated;     double determinant;      public double Determinant      {          get //asume thread-safe correctness in implementation of the getter          {              if (!determinantEvaluated)              {                   determinant = getDeterminant(this);                   determinantEvaluated = true;              }               return determinant;              }     } } 

UPDATE: Clarified the thread-safeness issue, as it was causing distraction.

like image 487
InBetween Avatar asked Jul 30 '15 11:07

InBetween


People also ask

Can immutable be changed?

In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.

What are immutable so that they Cannot be changed?

The term Mutable means "can change" and Immutable means "cannot change". An Immutable Object means that the state of the Object cannot change after its creation. Here the String is immutable means that you cannot change the object itself, but you can change the reference to the object.

What are immutable properties?

Immutable properties are properties that you can't change after you created the object. But in our case, you can change for example the FirstName property of a Friend at any time after you created the Friend object.


1 Answers

It depends.

If you are documenting for authors of client code or reasoning as an author of client code, then you are concerned with the interface of the component (that is, its externally observable state and behavior) and not with its implementation details (like the internal representation).

In this sense, a type is immutable even if it caches state, even if it initializes lazily, etc - as long as these mutations aren't observable externally. In other words, a type is immutable if it behaves as immutable when used through its public interface (or its other intended use cases, if any).

Of course, this can be tricky to get right (with mutable internal state, you may need to concern yourself with thread safety, serialization/marshaling behavior, etc). But assuming you do get it right (to the extent you need, at least) there's no reason not to consider such a type immutable.

Obviously, from the point of view of a compiler or an optimizer, such a type is typically not considered immutable (unless the compiler is sufficiently intelligent or has some "help" like hints or prior knowledge of some types) and any optimizations that were intended for immutable types may not be applicable, if this is the case.

like image 96
Theodoros Chatzigiannakis Avatar answered Sep 23 '22 07:09

Theodoros Chatzigiannakis