On Wednesday, 28 January 2015 at 09:44:29 UTC, zhmt wrote:
Sometime , I need to copy them:

thrift.Card tc;
....
db.Card dc;

dc.id = tc.id;
dc.pwd = tc.pwd;
...


It is boring coding, I want a solution to copy them automatically:
void copyObj(SRC,DEST)(SRC src,DEST dest)
{
        foreach (i, type; typeof(SRC.tupleof)) {
                auto name = SRC.tupleof[i].stringof;
__traits(getMember, dest, name) = __traits(getMember, src, name);
                writeln(name);
        }
}

Unfortunitely, it doesnt work,  how to improve it?

Assuming that the hibernated class isn't auto-generated and you can redefine its contents freely, the following style may be an alternative that works for you:

struct Foo {
public:
    string a;
    int b;
}

class FooClass {
public:
    union {
        struct {
            string a;
            int b;
        };
        Foo foo;
    }

}

void main() {
    Foo f = Foo("a", 10);
    FooClass c = new FooClass();
    c.foo = f;

    writefln("%s %s", c.a, c.b);
}

Probably the anonymous struct will break the UDAs, but it should be worth testing.

Reply via email to