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;
}
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
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
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
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
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 ---
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);
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
{
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
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
Hi All,
is there any (easy) way to clone an object or any other classes?
--Qian
11 matches
Mail list logo