Cloning in D
Michel Fortin
michel.fortin at michelf.com
Mon Sep 6 16:52:54 PDT 2010
On 2010-09-06 17:00:26 -0400, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> said:
> On 09/06/2010 02:56 PM, BLS wrote:
>> On 06/09/2010 05:15, dsimcha wrote:
>>> I've started playing around with Orange a little to see whether it
>>> would meet
>>> D's cloning needs.
>>
>> Clone respective member-wise clone ( I prefer copy and deep copy) should
>> be part of object. no std._tricks period.
>
> Though this might be the case in this particular instance, ideally a
> generally useful operation should be made available generically instead
> of encapsulated.
I'm under the impression that a too permissive generic implementation
of cloning is going to break things in various scenarios. What if your
object or structure is part of a huge hierarchy where things contains
pointers to their parent (and indirectly to the whole hierarchy), will
the whole hierarchy be cloned? What happens if your object or structure
maintains a reference to a singleton, will we get two instances of a
singleton? What if your object or structure implements the observer
pattern and contains a list of objects to notify when something
happens, will all the observers be cloned as well?
My understanding is that a data structure containing a pointer cannot
be cloned safely unless it contains some specific code to perform the
cloning. That's because the type system can't tell you which pointers
point to things owned by the struct/class and which one need to be
discarded when cloning (such as a list of observers, or the parents of
a hierarchy).
As for pointers to immutable things, the clone can keep pointing to the
same immutable data without affecting semantics, so there's no problem
there.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list