Why can't we define re-assignable const reference variable?

Derek Parnell derek at psych.ward
Mon Feb 18 05:52:07 PST 2008


On Mon, 18 Feb 2008 08:39:58 -0500, Denton Cockburn wrote:

> On Mon, 18 Feb 2008 19:36:31 +1100, Derek Parnell wrote:

>> import std.stdio;
>> class Foo
>> {
>>     const string _me;
>>     this(string name) { _me = name; writefln("Creating %s", _me); }    
>> }
>> void main()
>> {
>>    const(Foo) f;  // A mutable reference to const data.
>> 
>>    const(Foo) a = new Foo("one");
>>    const(Foo) b = new Foo("two");
>>    const(Foo) c = new Foo("three");
>>    
>>    f = a;
>>    f = b;
>>    f = c;
>>    
>>    writefln("%s", f._me);
>>    
>> }
>> 
> 
> That doesn't compile in 2.010

Damn! You're right. I was using an earlier version of D. However, if you
replace 'const(Foo)' with 'const(char)[]' is does compile. Which means that
a dynamic array, which is a reference type, can be mutable even though its
data is not.

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



More information about the Digitalmars-d mailing list