const?? When and why? This is ugly!

Derek Parnell derek at psych.ward
Sun Mar 8 00:54:03 PST 2009


On Sun, 08 Mar 2009 00:36:06 -0800, Walter Bright wrote:

> Derek Parnell wrote:
>> import std.stdio;
>> void main()
>> {
>>   int [] a = new int [1];
>> 
>>    a [0] = 1;
>> 
>>    invariant (int) [] b = cast (invariant (int) []) a;
>> 
>>    writef ("a=%s b=%s\n", a [0], b[0]);
>>    a [0] += b [0];
>>    writef ("a=%s b=%s\n", a [0], b[0]);
>>    a [0] += b [0];
>>    writef ("a=%s b=%s\n", a [0], b[0]);
>> }
>>    
>> The problem is that we have declared 'b' as invariant, but the program is
>> allowed to change it. That is the issue.
>> 
> 
> The following also compiles:
> 
> char c;
> int* p = cast(int*)&c;
> *p = 5;
> 
> and is clearly buggy code. Whenever you use a cast, the onus is on the 
> programmer to know what they are doing. The cast is an escape from the 
> typing system.

Walter, you have side-stepped the problem in question by talking about a
totally different problem.

Burtons code says "b is invariant", but the program allows it to be
changed. Your code does NOT say that any of those variables is invariant.
The problem is NOT with the cast (although that is a totally different
issue). The problem is that the code says "invariant" but the data gets
changed anyhow. The method of changing the data is not the issue. The issue
is that is gets changed at all.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list