Re: clone method of Object

2009-09-20 Thread jDavidls
This works very well (GDC 4.2.4): Object clone(Object object) { auto size = object.classinfo.init.length; object = cast(Object) ( (cast(void*)object) [0..size].dup.ptr ); // object.__monitor = null; return object; }

Re: clone method of Object

2009-04-15 Thread grauzone
bearophile wrote: grauzone: the clone method will only copy member b, but not a or c. A *good* implementation of this function seems fit to be added to Phobos. And serialization, and a complete reflection API. Bye, bearophile

Re: clone method of Object

2009-04-15 Thread bearophile
grauzone: > the clone method will only copy member b, but not a or c. A *good* implementation of this function seems fit to be added to Phobos. Bye, bearophile

Re: clone method of Object

2009-04-15 Thread grauzone
There are two things on my side: 1. Compiler refuses to clone private attributes. I have tried gdc/gdmd/dmd_v1 in Linux. It was changed in dmd later, and now you can access private attributes by using tupleof. I don't know when exactly it was changed, but it should work at least with dmd 1.039

Re: clone method of Object

2009-04-15 Thread Qian Xu
grauzone wrote: > > class A { > B b; > } > > class B { > A a; > } > > auto a = new A(); > auto b = new B(); > a.b = b; > b.a = a; > > Your recursive approach wouldn't quite work with that. Before cloning an > object, you'll first have to check if the object was already cloned. If > this is the

Re: clone method of Object

2009-04-15 Thread Qian Xu
grauzone wrote: > ... > cloned = clone(yourobject); Hi again. There are two things on my side: 1. Compiler refuses to clone private attributes. I have tried gdc/gdmd/dmd_v1 in Linux. 2. I have implemented an example. But some part not implemented. - code ---

Re: clone method of Object

2009-04-15 Thread grauzone
Qian Xu wrote: grauzone wrote: newobject.tupleof[i] = old.tupleof[i]; If the current value of tupleof[i] is an object, the object will be referenced, won't it? Shall I write: auto elem = old.tupleof[i]; static if (is(typeof(elem) == class)) { newobject.tupleof[i] = clone(elem);

Re: clone method of Object

2009-04-15 Thread Qian Xu
grauzone wrote: > newobject.tupleof[i] = old.tupleof[i]; If the current value of tupleof[i] is an object, the object will be referenced, won't it? Shall I write: auto elem = old.tupleof[i]; static if (is(typeof(elem) == class)) { newobject.tupleof[i] = clone(elem); } else {

Re: clone method of Object

2009-04-15 Thread grauzone
grauzone wrote: Qian Xu wrote: Hi All, is there any (easy) way to clone an object or any other classes? --Qian Simple answer: No. Complicated answer: Yes, but you have to write it yourself. Here's a nice starting point. You can use tupleof to get all members of a class. Note that this do

Re: clone method of Object

2009-04-15 Thread grauzone
Qian Xu wrote: Hi All, is there any (easy) way to clone an object or any other classes? --Qian Simple answer: No. Complicated answer: Yes, but you have to write it yourself. Here's a nice starting point. You can use tupleof to get all members of a class. Note that this doesn't deal with s

clone method of Object

2009-04-15 Thread Qian Xu
Hi All, is there any (easy) way to clone an object or any other classes? --Qian