Getting only the data members of a type

Artur Skawina art.08.09 at gmail.com
Sat Mar 31 21:09:32 PDT 2012


On 03/31/12 21:09, Ali Çehreli wrote:
> How can I determine just the data members of a struct or class, excluding the member functions? isCallable() comes short as member variable that have opCall() defined are also callable:
[...]
> Wait! I found a solution before sending this message. :P isIntegral() works:
> 
>         if (isIntegral!(typeof(mixin("S." ~ member)))) {
> 
> Now the output is:
> 
>     i is a member function
>     m is a member variable     <-- good but dubious
>   foo is a member variable
[...]

Don't forget about templates (appear as members too, but don't have a type).


This will print all fields of struct/class S:

   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


More information about the Digitalmars-d-learn mailing list