any news on const/invariant?

Derek Parnell derek at nomail.afraid.org
Mon Nov 26 15:37:03 PST 2007


On Mon, 26 Nov 2007 15:02:57 -0800, Walter Bright wrote:

> Derek Parnell wrote:
 
>>> 2) no more 'head const' or 'tail const', it's all just 'const'
>> 
>> Does that mean that if X is const then neither the bits in X and the bits
>> referenced by X can be modified (though the X symbol)?
> 
> Const and invariant are transitive. C++ const is not transitive, leading 
> to programmers establishing a *convention* for transitive const. But it 
> being an unenforced convention means it is not reliable.

And I assume that 'transitive' in this context means that if X is const
then it's members are const, and in turn their members are const, ... all
the way down.

Does this mean that the syntax 'const char[] X' and 'const (char)[] X' are
now the same thing?
 
 
>>> 3) ditto (2) for invariant
>> 
>> Does that mean that if X is invariant then neither the bits in X and the
>> bits referenced by X can be modified by anything?
> 
> Right.
> 
>>> It should be working as one would intuitively expect it to.
>> 
>> If the answer is 'yes' to the couple of questions above ...
>> (a) is there a form of the syntax that allows 'X' to be modified but not
>> what it references?
> 
> Yes, transitive const applies downwards, not upwards.

What would that syntax be? Can you give examples for arrays, AA, classes
and structs? 

>> (b) is there a form that prevents 'X' from being modified but allows what
>> it references to be modified?
> 
> No. There is no language typing support for 'logical const' (i.e. const 
> structures with mutable members). Many C++ people have argued 
> passionately for logical const, but I have become strongly convinced 
> that the notion is fatally flawed.

How would I specify that I have a buffer of bytes and I can't change its
location but I can change its contents?

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
27/11/2007 10:20:49 AM



More information about the Digitalmars-d mailing list