On Sunday, 1 May 2016 at 09:42:37 UTC, ParticlePeter wrote:
I am logging arbitrary POD struct types with member names and
data:
void printStructInfo( T )( T info ) {
foreach( i, A; typeof( T.tupleof )) {
enum attribName = T.tupleof[i].stringof;
writefln( "%s : %s", attribName, mixin( "info." ~
attribName ));
}
}
Is there is some other way to evaluate info.attribName without
using string mixins?
Cheers, PP
If you have `T info`, T.tupleof[n] will always match up with
info.tupleof[n]. You can think of `info.tupleof[n]` as being
rewritten by the compiler in-place as info.whateverFieldThatIs.
You might try this version (note the double {{ }} with static
foreach):
```d
void printStructInfo( T )( T info ) {
static foreach( i, A; info.tupleof ) {{
enum attribName = T.tupleof[i].stringof;
writefln( "%s : %s", attribName, info.tupleof[i] );
}}
}
```
Be advised that T.tupleof and __traits(allMembers, T) return two
different sets of things. allMembers will probably get you a lot
of stuff you don't want and will need to write checks to avoid.
Using .tupleof is a perfectly acceptable practice.