Phobos Members-Tuple function?

F i L witte2008 at gmail.com
Sat Jan 19 12:10:31 PST 2013


Is there a function in phobos which will return a Tuple of a 
Type's members, for use in a foreach statement?

I need this for collecting info on all Type Members's User 
Attributes. Currently, I have written a recursive function which 
does this, but I'm looking for a more elegant solution.

Let's say I have an Ship Actor class:

     class Ship : Actor
     {
         @Bind("Stage.update") void update()
         {
             // ...
         }

         @Bind("Canvas.draw") void draw()
         {
             // ...
         }
     }

and I want to iterator over all it's members.. and, in turn, 
iterate over all their attributes. If I use 
"__traits(derivedMembers, Ship)" it returns a Tuple of strings, 
which I can't use with __traits(getAttributes, ..). So what are 
my options here? Ideally, I'd like to be able to do this:

     auto ship = new Ship;
     enum mbrs = membersOf(Ship); // returns Tuple
     foreach (m; mbrs)
     {
         enum atrs = __traits(getAttributes, m);
         foreach (a; atrs)
         {
             if (is(a : Bind))
                 Engine.bindActors(ship, a.pattern);
         }
     }

Like I said, I've already accomplished this using recursion. I'm 
just wondering if there's an easier way (like above). Also, if 
recursive functions are required, I'd love to hear ideas on the 
best way to write a general purpose recursive function for this 
kind of thing.


More information about the Digitalmars-d-learn mailing list