static foreach

James Japherson JJ at goolooking.com
Wed Oct 10 23:03:38 UTC 2018


I have a static opApply in a class and I need to iterate over all 
the members statically using foreach

	// Iterates over all members
	static int opApply(int delegate(cPiece) dg)
	{
		alias T = typeof(this);
		foreach (m; __traits(derivedMembers, T))
		{
			import std.traits : hasStaticMember;
			static if (hasStaticMember!(T, m))
			{
			    mixin("dg(X."~m~");");
					
			}
		}
		return 0;
	
	}


This is not a problem using foreach.


But when I use static foreach it fails!!!

Says that it cannot interpret X(the class that contains the 
static opApply).

the opApply is static, it does everything at compile time, so why 
can't the foreach be statically used?

The whole point of using opApply was so that I wouldn't have to 
write all that code each time and I could just iterate over class 
members when I need to.



More information about the Digitalmars-d-learn mailing list