www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - deep copying / .dup / template object.dup cannot deduce function from argument types

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
Hi, I just wanted to naively copy an object and used:

a = myobj.dup;

and get the following error messages:

source/app.d(191): Error: template object.dup cannot deduce function 
from argument types !()(BlockV), candidates are:
/Library/D/dmd/src/druntime/import/object.d(1872):        object.dup(T 
: V[K], K, V)(T aa)
/Library/D/dmd/src/druntime/import/object.d(1908):        object.dup(T 
: V[K], K, V)(T* aa)
/Library/D/dmd/src/druntime/import/object.d(3246):        
object.dup(T)(T[] a) if (!is(const(T) : T))
/Library/D/dmd/src/druntime/import/object.d(3262):        
object.dup(T)(const(T)[] a) if (is(const(T) : T))
/Library/D/dmd/src/druntime/import/object.d(3273):        object.dup(T 
: void)(const(T)[] a)

Hmm... so, is .dup not the way to go?

And, am I correct, that a deep-copy needs to be hand coded?

-- 
Robert M. MŁnch
http://www.saphirion.com
smarter | better | faster
Dec 13 2015
next sibling parent Alex Parrill <initrd.gz gmail.com> writes:
On Sunday, 13 December 2015 at 18:54:24 UTC, Robert M. M√ľnch 
wrote:
 Hi, I just wanted to naively copy an object and used:

 a = myobj.dup;

 and get the following error messages:

 source/app.d(191): Error: template object.dup cannot deduce 
 function from argument types !()(BlockV), candidates are:
 /Library/D/dmd/src/druntime/import/object.d(1872):        
 object.dup(T : V[K], K, V)(T aa)
 /Library/D/dmd/src/druntime/import/object.d(1908):        
 object.dup(T : V[K], K, V)(T* aa)
 /Library/D/dmd/src/druntime/import/object.d(3246):        
 object.dup(T)(T[] a) if (!is(const(T) : T))
 /Library/D/dmd/src/druntime/import/object.d(3262):        
 object.dup(T)(const(T)[] a) if (is(const(T) : T))
 /Library/D/dmd/src/druntime/import/object.d(3273):        
 object.dup(T : void)(const(T)[] a)

 Hmm... so, is .dup not the way to go?

 And, am I correct, that a deep-copy needs to be hand coded?
`dup` is an array method; it only works on arrays. Structs are value types, so `a = b` will "duplicate" a struct. For classes, you will need to define your own clone function.
Dec 13 2015
prev sibling parent rumbu <rumbu rumbu.ro> writes:
On Sunday, 13 December 2015 at 18:54:24 UTC, Robert M. M√ľnch 
wrote:
 Hi, I just wanted to naively copy an object and used:

 a = myobj.dup;

 [...]
A naive clone method for objects: https://github.com/rumbu13/sharp/blob/c34139449a078618e807a3f206541656df1bea6a/src/system/package.d#L46
Dec 14 2015