Extended Type Design.

James Dennett jdennett at acm.org
Tue Mar 20 22:55:02 PDT 2007


Don Clugston wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> Don Clugston wrote:
>>> Assuming that 'invariant' = really constant, 'const' = C++
>>> pseudo-const...
>>
>> Yah.
>>
>>> It's better than super const. However:
>>> (1) I would want to go through all my existing D code and change 100%
>>> of my usages of 'const' to 'invariant'.
>>
>> Great to hear that!
> 
> Sorry, I didn't explain myself very well. I meant that this proposal
> breaks 100% of all existing D uses of const.
> 
>  >> (3) I concede the problem of association with 'readonly' and ROM. But
>>> the association between 'const' and 'constant' is pretty strong.
>>
>> I think there's universal agreement that you can't express tersely the
>> two notions "const-as-in-C++" and "const-as-in-we-really-mean-it".
>> Probably it's best to just go with two terms and move on.
> 
> I completely agree with this. The gripe I have is that 'const' is a
> completely inappropriate term for 'const-as-in-C++'. It's a bizarre
> misnomer to use 'const' to mean "don't touch". 

Indeed, and seems to represent a misunderstand of what
const *does* mean in C++.

In this context:
  int const x(3);
it means "x is constant, i.e., cannot be changed".  In
this context:
  int const *y(&x);
it means "y is a pointer to a (possibly constant) int".
Without the const, y would not be allowed to point to
a constant int.  It doesn't mean "don't touch" -- it
means "cannot be used to alter", i.e., "leaves const
if indeed it was const".  It's closer to "won't touch".

It's clear though that the meaning confuses too many
people.  What's not clear to me is whether the proposed
terminology for D is any better.  Certainly much of
the discussion seems to be based on misstatements of
how C++ works.  There's scope to do better than C++,
but it does require understanding that on which we hope
to improve.

-- James



More information about the Digitalmars-d mailing list