thoughts on immutability in D

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 23 16:13:32 PDT 2011


On Friday, September 23, 2011 15:58 Andrej Mitrovic wrote:
> On 9/24/11, Peter Alexander <peter.alexander.au at gmail.com> wrote:
> > On 23/09/11 6:48 AM, Walter Bright wrote:
> >> On 9/22/2011 4:39 AM, bearophile wrote:
> >>> Walter:
> >> "logical const" in C++ is faith-based programming.
> > 
> > I think you're exaggerating it's uselessness.
> > 
> > It's faith-based as much as 'walking down the street assuming that the
> > next person won't stab you' is faith-based. There's no guarantee, but
> > 99% of the time your "faith" is well placed.
> 
> So then don't use const if u need caching and use convention if your
> faith is well placed!

const in C++ really does help to catch bugs. It's definitely better than 
nothing. I do _not_ think that it is worthless like Walter at least gives the 
impression that he thinks.

Now, I _do_ think that on the whole, D's const is better. The transitiveness 
of const is a major improvement IMHO, and the caching thing, while annoying, 
isn't that big a loss in my experience. I expect that with Peter's gaming 
background, it's much more of an issue for him. I think that it's at least 
theoretically possible to get a caching mechanism to work with const, which 
would overcome that portion of the logical const problem, but the question is 
how best to do it and whether the added complication is worth the cost. 
Certainly, it's the sort of thing that we should look into for D3 when we get 
that far. But for now, on the whole, D's const is a definite improvement over 
C++'s const, and you just don't use const in D in quite as many places as you 
would have in C++.

But while C++'s const is not as good as D's const, it's still very worthwhile 
IMHO.

- Jonathan M Davis


More information about the Digitalmars-d mailing list