any news on const/invariant?

Walter Bright newshound1 at digitalmars.com
Mon Nov 26 16:36:17 PST 2007


Derek Parnell wrote:
> 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.

Yes.


> Does this mean that the syntax 'const char[] X' and 'const (char)[] X' are
> now the same thing?

No. "const char[] X;" and "const(char[]) X;" mean 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?

Just put () around the part of the type you wish to be const. See above 
examples.

>>> (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?

You can't. A good question would be what would be the purpose of such?



More information about the Digitalmars-d mailing list