proposal for general dup function

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Dec 10 11:46:18 PST 2012


On 12/10/12 1:55 PM, Jacob Carlborg wrote:
> On 2012-12-10 17:06, Dan wrote:
>
>> 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.
>
> I'm pretty sure it can't be done.

Just like Dan I thought it can be done but actually ownership is 
impossible to establish in general. Consider:

class List(T) {
     List next;
     T payload;
     ...
}

versus

class Window {
     Window parent;
     ...
}

It's pretty obvious from the name that duplicating a List would entail 
duplicating its tail, whereas duplicating a Window would entail just 
copying the reference to the same parent. However, at the type level 
there's no distinction between the two members.

Now, user-defined attributes could change the playfield radically here. 
Consider we define things like @owned and @foreign that would inform the 
gdup or deepdup function appropriately:

class List(T) {
     @owned List next;
     T payload;
     ...
}

versus

class Window {
     @foreign Window parent;
     ...
}

Now a generic deep duplication function has enough information to 
duplicate things appropriately.


Andrei


More information about the Digitalmars-d mailing list