It makes the code icky and hard to debug, and you can simply return new immutable objects for every state change.
EDIT: why not just create a new object and reassign variable to point to the new object
It makes the code icky and hard to debug, and you can simply return new immutable objects for every state change.
EDIT: why not just create a new object and reassign variable to point to the new object
As others have pointed out, there is the issue of breaking references to objects.
There can also be a lot of memory thrashing if you have to keep reallocating and copying objects all the time. To some extent, that may be mitigated using an internment scheme for common values. In Python, for example, integers are immutable but they intern something like the first 100 or so iirc? But that doesn’t work well for everything.
Any container you want to populate dynamically should probably be mutable to avoid O(N²) nastiness.