proposal for general dup function

deadalnix deadalnix at gmail.com
Mon Dec 10 11:50:49 PST 2012


On Monday, 10 December 2012 at 19:46:18 UTC, Andrei Alexandrescu 
wrote:
> 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.
>

Unless the tail is immutable, but in general yes.

> 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.
>

That is indeed a perfect use case for attributes.


More information about the Digitalmars-d mailing list