January 28, 2015
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.
January 29, 2015
On Wednesday, 28 January 2015 at 23:34:10 UTC, Chris Williams wrote:
> 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.

The hibernated class is not auto-generated yet. I think this is a good idea too.

January 29, 2015
On Wednesday, 28 January 2015 at 14:59:52 UTC, Ali Çehreli wrote:
> name must be 'enum':
>
> On 01/28/2015 06:34 AM, zhmt wrote:
>> void getT(SRC,DEST)(SRC src,DEST dest)
>> {
>>     foreach (i, type; typeof(SRC.tupleof)) {
>>         string name = SRC.tupleof[i].stringof;
>
>         enum name = SRC.tupleof[i].stringof;
>
>>         __traits(getMember, dest, name) =  __traits(getMember, src, name);
>>          writeln(name);
>>     }
>> }
>>
>> when I write the code above, the compile complains that:
>>
>> source/app.d(14): Error: variable name cannot be read at compile time.
>
> Ali

@Ali, I have test the enum name declaration, it works well, Thx for your help.
1 2
Next ›   Last »