Phobos Members-Tuple function?
Ali Çehreli
acehreli at yahoo.com
Sat Jan 19 18:41:08 PST 2013
On 01/19/2013 12:10 PM, F i L wrote:
> Is there a function in phobos which will return a Tuple of a Type's
> members, for use in a foreach statement?
The following program produces this output:
member update
attribute Bind("Stage.update")
Binding actor with pattern Stage.update
member draw
attribute Bind("Canvas.draw")
Binding actor with pattern Canvas.draw
member toString
member toHash
member opCmp
member opEquals
member Monitor
member factory
I had to use a mixin and had to move __traits(allMembers) into the
foreach loop. It was necessary so that 'm' could be evaluated at compile
time. I can kind of see why but one would expect your 'enum mbrs' to
work as well.
import std.stdio;
interface Actor
{}
struct Bind
{
string pattern;
}
class Ship : Actor
{
@Bind("Stage.update") void update()
{
// ...
}
@Bind("Canvas.draw") void draw()
{
// ...
}
}
class Engine
{
static bindActors(Actor, string pattern)
{
writefln(" Binding actor with pattern %s", pattern);
}
}
void main()
{
auto ship = new Ship;
alias T = typeof(ship);
foreach (m; __traits(allMembers, T))
{
writefln("member %s", m);
enum atrs = __traits(getAttributes, mixin(T.stringof ~ "." ~ m));
foreach (a; atrs)
{
writefln(" attribute %s", a);
if (is(typeof(a) == Bind))
Engine.bindActors(ship, a.pattern);
}
}
}
Ali
More information about the Digitalmars-d-learn
mailing list