== Quote from Andrei Alexandrescu (seewebsiteforem...@erdani.org)'s article
> On 09/05/2010 10:15 PM, dsimcha wrote:
> > I've started playing around with Orange a little to see whether it would 
> > meet
> > D's cloning needs.  IMHO one must-have feature for proper cloning that truly
> > "just works" is full aliasing preservation.
> What do you mean by aliasing preservation? I'd think you want no
> aliasing between the clone and the original. (Perhaps you mean that the
> object graph of the clone and original look the same?)

Right.  This means if you had, for example, two slices that point to overlapping
regions of memory anywhere in your original object graph, they'd overlap instead
of pointing to distinct regions in your cloned object graph.

> First off, let's see what cloning methods we have:
> 1. We know how to clone built-in types except pointers. (Arrays are
> cloned by duplicating the array proper and cloning each element.)

I think we could handle even pointers in some limited cases.  If it's allocated 
on
the GC heap (so we can easily find the bounds of the memory region) and the type
pointed to doesn't have any additional indirection (so we don't need to follow
possibly invalid pointers), then we just duplicate the entire GC memory block.

For pointers to non-primitives, we could arbitrarily assume that the pointer
points to exactly one instance, and is being used simply as a reference.

> 2. Structs can be cloned with either this(this) or by cloning each of
> their fields.

Yeah, structs with postblits are a huge, hairy PITA.  If the struct is using
reference counting, cloning the entire object graph reachable from the struct 
will
yield an incorrect reference count field.  I think that for structs we should
always assume that transitively duplicating the entire object graph field by 
field
is a safe way to clone the struct, unless it comes with its own clone() method.
this(this) can be used for too many other things, like the aforementioned ref
counting.

> 3. For classes we can define a conventional method/interface, e.g.
> clone/Cloneable.

Right, and there should be a mixin to implement this for the simple cases, 
rather
than making the programmer do it him/herself.

One concern w/ opaque clone() calls is establishing a protocol by which aliasing
(array slice overlapping, etc.) information can be shared between the clone()
function and the main deepdup() function, as well as among different clone 
functions.

Reply via email to