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

Derek Parnell derek at nomail.afraid.org
Mon Feb 18 00:36:31 PST 2008


On Mon, 18 Feb 2008 07:12:08 +0000, Janice Caron wrote:

> I say again, there is no way to express "mutable reference to const
> data" in D

Of course you can. I do it all the time. But maybe I don't understand what
you are saying ;-)

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);
   
}


What I can't get D to do easily is declare a const reference to mutable
data. Yes, I know there are ways to trick D to get around this but its not
as nice as just letting the programmer achieve their goals with simpler,
more intuitive syntax.

eg..
   const nonconst(Foo) f; // 'f' is const but it's data is not.

And I know the argument "But what purpose what that really serve?". The
answer to that is "doing it another way is more complex".

But anyhow, I know its not going to happen so my part in the discussion can
end here.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
18/02/2008 7:04:36 PM



More information about the Digitalmars-d mailing list