Getting only the data members of a type

Jacob Carlborg doob at me.com
Sun Apr 1 05:09:57 PDT 2012


On 2012-04-01 08:18, Ali Çehreli wrote:
> On 03/31/2012 09:09 PM, Artur Skawina wrote:

>  >
>  > enum s = cast(S*)null;
>  > foreach (i, m; s.tupleof) {
>  > enum name = S.tupleof[i].stringof[4..$];
>  > alias typeof(m) type;
>  > writef("(%s) %s\n", type.stringof, name);
>  > }
>  >
>  > Real Programmers don't use std.traits. ;)
>  >
>  > artur
>
> Your method works but needing to iterate on a struct variable by
> s.tupleof and having to use the struct type as S.tupleof in the loop
> body is strange.

Yeah, it's a bit strange. One could think that it would be possible to 
use "m.stringof" but that just returns the type. Instead of using 
"s.tupleof" it's possible to use "typeof(S.tupleof)".

Have a look at:

https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L212

It's possible to get the type of a field as well, based on the name:

https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L237

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list