Static Foreach

Shammah Chancellor anonymous at coward.com
Sun Nov 24 09:40:14 PST 2013


It seems that DMD already interprets some foreach's at compiletime if 
the argument is known at compile time:

eg:

	string FooString()
	{
		string stuff = "";
		foreach(member, __traits( allMembers, moduleName)
		{
			stuff ~= member;
		}
		return stuff;
	}

mixin(FooString());

However, for non-string templates.   They have to be written in a 
recursive form, which can be particularly difficult in some cases.

	template FooTemplate()  //This code is totally made up and not meant 
to do anything useful, or necessarily be valid.
	{
		auto FooTemplate = TypeTuple!()
		static foreach(member, __traits( allMembers, someClass)
		{
			FooTemplate = TypeTuple!(FooTemplate, __traits(getMember, someClass, 
member));
		}
	}

What's the consensis on something like this?



More information about the Digitalmars-d mailing list