proposal for general dup function

Dan dbdavidson at yahoo.com
Mon Dec 10 08:06:42 PST 2012


On Monday, 10 December 2012 at 15:36:44 UTC, Joseph Rushton 
Wakeling wrote:
> On 12/09/2012 03:45 PM, Dan wrote:
>> Phobos can and should have a general dup function, capable of 
>> duping (i.e.
>> recursive deep copy) structs without requiring any effort from 
>> struct
>> developers. This can be done to cover the vast majority of 
>> object copy issues
>> for structs
>
> We already had some discussion about your gdup on d-learn, but 
> just for clarity -- it's possible to do a gidup as well as 
> gdup, correct?

I think so. Here is a claim I think is true: gdup must do full 
deep copy to be safe and guarantee the transitive 
const/immutable. If it is implemented such that there are no 
casts, then the compiler does its job and ensures everything is 
good. If there are casts they need to be deemed safe - I have one 
cast to work around issues in associative array iteration. 
Assuming that is fine - I claim that gidup or igdup or whatever 
can just be:

@property auto gidup(T)(const ref T t) {
   immutable(T) result = cast(immutable)(t.gdup);
   return result;
}

     auto another = c.gidup;
     pragma(msg, typeof(another));
     assertNotEquals(another.b.a.c.ptr, c.b.a.c.ptr);
     assert(0==typesDeepCmp(another,c));

That is, since gdup does a full deep copy, casting the result to 
immutable is fine as there is no aliasing.

Thanks,
Dan


More information about the Digitalmars-d mailing list