thoughts on immutability in D

Walter Bright newshound2 at digitalmars.com
Fri Sep 23 19:42:24 PDT 2011


On 9/23/2011 6:55 PM, Jonathan M Davis wrote:
> True, but it doesn't just happen. You have to choose to make a variable
> mutable.

Which you *must* do for logical const. And, of course, anything beyond the first 
level is not const at all, and there's NO WAY to say it is const.


> _Some_ effort must be put in to circumvent const. Until you do that,
> const protects it from be changed.

There's no way to detect who or what is changing it.


> By no means am I claiming that C++'s const is without flaws. I'm just claiming
> that it's not worthless, and you seem to be claiming that it's worthless.

I think it is worthless because:

1. a number of C++ programmers I've talked to who relied on being able to change 
const objects and insisted that was a feature.

2. the pervasive misconceptions about what C++ const guarantees and what it 
doesn't, even among C++ committee members.

3. the complete uselessness C++ const has for multithreaded programming.

4. the pervasiveness of the Double Checked Locking Bug, which is one of the 
consequences of (3).

5. it's head-const only. Anything beyond one level of indirection is completely 
const-free. Only trivial data structures are one level.


More information about the Digitalmars-d mailing list