const in dmd v2.011

Jesse Phillips jessekphillips at gmail.com
Wed Feb 20 17:21:01 PST 2008


On Thu, 21 Feb 2008 11:44:14 +1100, Derek Parnell wrote:

> On Wed, 20 Feb 2008 19:03:56 -0500, Jason House wrote:
> 
>> It seems like you may have read some very early D 2.x docs on const and
>> not read the later D 2.x docs.  The const design has changed a lot.
> 
> Thanks Jason. I know that keeping up with the changes in const theory
> and practice is a full time job in itself ;-) but I had been aware of
> these later tweaks.
> 
>> Derek Parnell wrote:
>>> Observations:
>>> ** It appears that there is no practical difference between 'const(C)
>>> c' and 'const C c'.
>> 
>> This is the definition of transitive const.
> 
> Yes it is. What I was bring to attention is the potentially confusing
> syntax options that are now available.
>  
>>> ** It appears that 'const(char[])' and 'const const(char)[]' are
>>> equivalent and also not implemented well (or the error messages are
>>> just bad).
>> 
>> const(char)[] means the char's within the array can't be changed, but
>> the array itself can change (ie. length changes, concatenation)
> 
> Yes, I know what it means. Again what I'm bring attention to is the
> confusing nature of this syntax.
> 
>    const(char)   X; // 'X' is const.
>    const(char)[] X; // 'X' is not const.
> 
> also this ...
> 
>    const(char)* k; // 'k' is a mutable pointer to immutable data.
>    const(char*) l; // 'l' is an immutable pointer to immutable data.
> 
> The forms 'const(char)[]' and 'const(char)*' ignore transitivity while
> other forms enforce it. It could be confusing, no?
> 
> Also the forms 'const const(X)[]', 'const const(X[])', and 'const(X[])'
> appear to be synonymous, another way to confuse people.

Personally the const(char)* and const(char)[] make sense in their 
meaning. Not too the definition given of transivity:

"transitive, which means that any data reachable through an invariant 
reference is also invariant, and likewise for const."

The use of the word reference shows that it does not apply to pointers. 
This however is confusing to those that do not see a difference, I do it 
my self at times.


More information about the Digitalmars-d-learn mailing list