const again

Walter Bright newshound1 at digitalmars.com
Thu Dec 6 16:07:51 PST 2007


Derek Parnell wrote:
> On Thu, 06 Dec 2007 13:33:46 -0800, Walter Bright wrote:
> 
>> As a result of all the const comments here, and some serious semantic 
>> problems discovered (dang it's hard to think of everything in advance), 
>> it seems there needs to be another tweak of the const behavior.
> 
> I'm glad you're flexible ;-)

My overriding concern with const is to have a useful, correct, and 
workable system!

> 
>> So, we're going to try a new, simpler regime:
>> 	const T x;
>> is semantically identical to:
>> 	const(T) x;
>> and the type of x is const(T).
> 
> Which means for reference types, 'x' is modifiable but what it references
> is not modifiable.

No, it means whatever part of the type is inside the () is not modifiable.

> And for non-reference types, 'x' is not modifiable. So
> in some cases 'x' can be changed but in some cases it can't. Is that going
> to be a problem?
> 
>    const int   x;  // Can't change 'x'
>    const int[] y;  // Can change 'y' but not y[n]

With these, neither x, y, nor y[n] are mutable.


>   const C[] a;

a is a const array of const C.

>   const (C)[] a;

a is an array of const C.

>   const (C[]) a;

a is a const array of const C.

>   const ((C)[]) a;

a is a const array of const C.



More information about the Digitalmars-d mailing list