Hacker News new | ask | show | jobs
by dfgdghdf 2467 days ago
Should 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!
1 comments

Can'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.
Technically copy-on-write is an implementation detail.

It may or may not be implemented this way.

I mean that CoW classes like Qt's QStringList do the same data sharing without being immutable.
Admittedly, it's pretty easy to accidentally make unnecessary non-const accesses in modern C++. An immutable API would certainly avoid a lot of qAsConst casts.