4 ms·
The simplest answer is that immutability allows you to share data around without worrying about it being modified. In the OOP world, you might have a "getter"
by dfgdghdf 7y ago
The simplest answer is that immutability allows you to share data around without worrying about it being modified.
In the OOP world, you might have a "getter" for a property (e.g. a list of some kind) that you don't want readers to be able to modify.
Perhaps "set" was a poor name here, since it usually means something else in a C++ context. I quite like "with".
- dfgdghdf 7y agoShould add that C++ has const, making some of this irrelevant. In the C++ case immutability is about making minor changes to a structure more efficient by reusing the previous version. This is only possible with immutability because you need to be sure that your "base" structure won't change underneath you!
- slavik81 7y agoCan't copy-on-write achieve the same thing? If you keep a reference count, you can know whether it's safe to mutate the underlying data, or whether you need to make a copy first.
- heavenlyblue 7y agoTechnically copy-on-write is an implementation detail. It may or may not be implemented this way.