Copy constructor in D. Why it is necessary to have it.

bearophile bearophileHUGS at lycos.com
Thu Oct 2 14:58:26 PDT 2008


Sergey Gromov:
> Deep copy is not possible without knowing the object structure.  How 
> about opDup?  :)

An opCopy() / opDeepcopy looks useful (not opDup because in D special names are named about their meaning and not their syntax).
In Python there are the standard special methods __copy__ and __deepcopy__ for that purpose (during deepcopying the system takes care of eventually present loops, this will be important in D too).

Related to the copy, a opGetstate() / opSetstate() may be useful to serialize / deserialize objects on disk.
In Python they of named __setstate__ / __getstate__, when they are present the pickle function uses them, otherwise it just reads/writes all the object attributes.

Bye,
bearophile



More information about the Digitalmars-d mailing list