const?? When and why? This is ugly!

Walter Bright newshound1 at digitalmars.com
Sat Mar 7 23:40:52 PST 2009


Burton Radons wrote:
> Walter Bright Wrote:
> 
>> Burton Radons wrote:
>>> This wouldn't be too bad because const is worthless when
>>> optimising, but if invariant is going to be given any weight then
>>> we must never cause data to be casted to invariant unless if it's
>>> actually invariant data. So, the sensible default is "const
>>> (char) []" for strings, a selection of aliases in object.d for
>>> the others, and safe casting templates in object.d.
>> What I interpret from this is that you see strings as fundamentally
>>  mutable character arrays, and sometimes in special cases you can
>> make them immutable. I propose turning that view on its head -
>> regard strings as fundamentally immutable, and having a mutable
>> char array is a rare thing that only appears in isolated places in
>> the program.
> 
> No, I don't. You are misunderstanding me, and I'm not sure why or
> how.

I guess I just cannot figure out where you're coming from.

> Here's a (contrived) example of where my concern may come into
> play:
> 
> int [] a = new int [1];
> 
> a [0] = 1;
> 
> auto b = cast (invariant (int) []) a;
> 
> a [0] += b [0]; a [0] += b [0]; writef ("%s\n", a [0]); // Normal
> result: 4. // Optimiser which assumes invariant data can't change: 3
> 
> Yes, the code is an abuse of the const system. THAT'S EXACTLY MY
> POINT. Casting mutable data to invariant leads to situations like
> these. Only data which will never change can be made invariant.
> Putting "alias invariant (char) [] string" in object.d induces these
> situations and makes it seem like it's a good idea.

I'm still not understanding you, because this is a contrived example 
that I cannot see the point of nor can I see where it would be 
legitimately used.



More information about the Digitalmars-d mailing list