Thread overview
how do I read a class member's value using traits?
Dec 16
Marc
Dec 16
Marc
December 16
I need to give a class C, read all user-defined members of it, both name and value dynamically. for example:

> class C {
>   string a;
>   string b;
>   string c;
> }

then

> Class c = new C();
> // set c members...
> enum string[] members = [__traits(allMembers, C)];
> foreach(string member; members) {
>	string value = __traits(getMember, c, member);
>	doSomething(member, value);
>}

I get this error:

> Error: variable member cannot be read at compile time
> Error: string expected as second argument of __traits `getMember` instead of `__error`

why can't member in foreach(string member; members) be read at compile time? I've also tried

> foreach(enum string member; members)
and
> static foreach(string member; members)

but it result a lot of error messages.



December 15
On Saturday, December 16, 2017 03:34:43 Marc via Digitalmars-d-learn wrote:
> I need to give a class C, read all user-defined members of it,
>
> both name and value dynamically. for example:
> > class C {
> >
> >   string a;
> >   string b;
> >   string c;
> >
> > }
>
> then
>
> > Class c = new C();
> > // set c members...
> > enum string[] members = [__traits(allMembers, C)];
> > foreach(string member; members) {
> >
> > string value = __traits(getMember, c, member);
> > doSomething(member, value);
> >
> >}
>
> I get this error:
> > Error: variable member cannot be read at compile time Error: string expected as second argument of __traits `getMember` instead of `__error`
>
> why can't member in foreach(string member; members) be read at
> compile time? I've also tried

You're trying to read a runtime value at compile time, and that's not going to work. c doesn't exist until runtime, so none of its members exist until runtime. You can introspect on the type C, but the variable c is a runtime entity.

- Jonathan M Davis

December 16
On Saturday, 16 December 2017 at 03:48:01 UTC, Jonathan M Davis wrote:
> On Saturday, December 16, 2017 03:34:43 Marc via Digitalmars-d-learn wrote:
>> I need to give a class C, read all user-defined members of it,
>>
>> both name and value dynamically. for example:
>> > [...]
>>
>> then
>>
>> >[...]
>>
>> I get this error:
>> > [...]
>>
>> why can't member in foreach(string member; members) be read at
>> compile time? I've also tried
>
> You're trying to read a runtime value at compile time, and that's not going to work. c doesn't exist until runtime, so none of its members exist until runtime. You can introspect on the type C, but the variable c is a runtime entity.
>
> - Jonathan M Davis

You're right. I changed to generate code to do it properly at runtime, something like this:

> enum string[] members = [__traits(derivedMembers, Field)] [0 .. $ - 1];
>		static foreach(enum string member; members) {
>			doSomething(member, __traits(getMember, fields, member));
		}

Not sure if best approach. note: I know [0 .. $ -1] is probably wrong. I'll fix it