Const sucks

Walter Bright newshound1 at digitalmars.com
Mon Sep 10 15:35:24 PDT 2007


Sean Kelly wrote:
> Walter Bright wrote:
>>
>> So, what we are left with is:
>>
>> o  no more final
> 
> By this you meant that "final" in 2.0 would work exactly as in 1.0, 
> correct?

Yes. There'll still be final classes and final virtual functions.

>> o  const and invariant now mean "fully const" and "fully invariant", 
>> where fully means "head and tail combined":
>>
>> const int x = 0;   // x is constant
>> const int* p = &x;  // neither p nor *p can be changed
>> const(int*) p = &x;  // neither p nor *p can be changed
>> const(int)* p = &x;  // p can change, *p cannot be changed
> 
> So the presence of parenthesis would be simply to limit the extent of 
> the type to which "const" applies?

Yes. Think of the () as presenting an 'argument' to be const-ified.


>> o  tail const of a struct would have to be done by making the struct a 
>> template:
>>
>>   struct S(T) { T member; }
>>   S!(int)   // tail mutable
>>   S!(const(int)) // tail const
>>
>> o  one can construct a template to generically produce tail const or 
>> tail invariant versions of a type.
> 
> Could you please explain this further?  Why would templates be needed in 
> the above two points?

Think of a struct which was your own implementation of arrays:

struct Array(T)
{
     T[] a;
}

To create a tail const array, instead of const(T)[], we'd do 
Array!(const(T)).



More information about the Digitalmars-d mailing list