loop through specific class members

Christopher Wright dhasenan at gmail.com
Tue Jan 20 16:42:46 PST 2009


Jarrett Billingsley wrote:
> On Tue, Jan 20, 2009 at 10:29 AM, Trass3r <mrmocool at gmx.de> wrote:
>> It seems like there is no way to automatically get the class methods in D1
>> currently?!
>> __traits isn't supported, std.traits doesn't give anything usable, .tupleof
>> only gets the fields (plus only giving an ExpressionTuple for classes).
> 
> You're correct.  __traits was introduced in D2 mostly because D1's
> compile-time introspection is so paltry.

But unless you're doing very basic stuff or only touching a few classes, 
compile-time introspection ends up being quite costly. I want runtime 
reflection for that reason.

On the other hand, you can get the non-final, non-private methods of a 
class with something like:
foreach (member; __traits (allMembers, Class))
{
	foreach (overload; __traits (getVirtualFunctions, Class, member))
	{
		// do stuff
	}
}

Naturally, __traits and foreach don't mix, so you'll have to use 
template recursion. This is pretty damn ugly.

Note that getVirtualFunctions returns methods that are not virtual. You 
need to use isVirtualFunction to determine whether a method is virtual.


More information about the Digitalmars-d-learn mailing list