3 ms·
Only the map variable points to an immutable object. The second line reassigns that variable to a new (also immutable) Map instance, which is an exact copy of t
by rtfeldman 12y ago
Only the map variable points to an immutable object. The second line reassigns that variable to a new (also immutable) Map instance, which is an exact copy of the original except that b has been changed.
Does that make sense?
- rakoo 12y agoPut further, Rich Hickey had a talk about this, I don't remember which one. Most languages (unfortunately those we usually start with) conflate _identity_ and _value_, even though they're completely different notions. It was ok with mutable structures, but immutability puts the problem on the front. A map is a value; it never changes. When you add something to a map, the old map doesn't change, only the new one does (in that it is created). We have as many values as there are transformations. Of course libraries and GCs will make sure that we use physical resources as efficiently as possible. An identity, on the other hand, links a _label_ to a _value_ at a certain point in _time_: - the label 'mymap' is first associated with the value '{"a":true}', then with the value '{"a":false}' - the label 'Tim's Birthday' is associated with the value '1970/01/13' forever (people's birthday rarely change) - the label 'President of the US' is associated with the value 'Barack Obama' for the moment, it was different a few years ago, and will be different in a few years. Going back to the example, we have a single identity, with label 'map', that takes 2 different values over time.