Examining Members of a module at Compile Time

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 29 16:13:47 PDT 2014


I'd like to get a list of all classes in the current module, so I 
came up with this code:

class Test {}

class TestChild: Test {}

class TestChildChild: TestChild {}

void main()
{
	foreach (item; __traits(allMembers, mixin(__MODULE__)))
	{
		static if (is(mixin(item) == class))
		{
			pragma(msg, item);
  		}
	}
}

This code doesn't work, complaining about `mixin(item)`. So them 
I thought I'd try:

static if (is(__traits(getMember, mixin(__MODULE__), item) == 
class))
{
     pragma(msg, item);
}

Which the compiler similarly complains about. I also tried 
instead assigning the result of the __traits call to an alias, 
which the compiler didn't like either. Finally I happened on 
something that worked:

const isModuleClass = mixin("is(" ~ item ~ " == class)");
static if (isModuleClass)
{
     pragma(msg, item);
}

The mixin looks quite ugly, of course, and I'd like to know if 
there's a better way to turn the string from __traits(allMembers, 
...) into a symbol so it can be used in typeof expressions and 
other things.


More information about the Digitalmars-d-learn mailing list