thoughts on immutability in D

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 23 20:11:05 PDT 2011


On Friday, September 23, 2011 19:42:24 Walter Bright wrote:
> 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.

Well, I grant you that all of those are issues, but I don't think that they 
makes C++'s const worthless. Const has caught bugs in my code for me. I have 
found it to be useful in the projects that I've worked on. So, we're obviously 
just going to have to agree to disagree on this, and I think that a lot of C++ 
programmers are going to disagree with you (including many who understand 
exactly what const does and doesn't guarantee).

- Jonathan M Davis


More information about the Digitalmars-d mailing list