Inability to dup/~ for const arrays of class objects

Ali Çehreli acehreli at yahoo.com
Tue May 28 18:19:49 PDT 2013


On 05/28/2013 06:04 PM, Peter Williams wrote:

 > I'm implementing sets and the concept is sets of objects not sets of the
 > values in the objects.  I want to be able to initialize sets using
 > arrays and I'm promising that I won't change the array or its contents.
 >   I'm also trying to promise that I won't (inside the set
 > implementation) make any changes to individual objects.  But I'm not
 > promising that they won't be changed by other code that has access to
 > them.

Then I still think that it is related to the fact that a const class 
variable (not object) cannot refer to another object, which I think 
should be possible. Others think so too:

   http://forum.dlang.org/thread/bug-5325-3@http.d.puremagic.com/issues/

I remember the discussions on that topic and various syntax proposals 
but I don't remember why class variable assignment syntax would not work:

     // C is a class
     const(C) c = new const(C);

     // c promises that it won't mutate the object.
     // Why can't it promise to not mutate another object?

     c = new const(C);  // <-- compilation error

I don't see the rationale for disallowing the last line. I think it 
should work without any syntax change. Merely the class handle is being 
changed.

In this case it looks like "turtles all the way *up*" because just 
because the object is const, the handle is treated as const as well.

Ali
My head is thick today. :p



More information about the Digitalmars-d mailing list