Reflection to detect public/private/etc?

Nick Sabalausky a at a.a
Fri Aug 12 14:59:38 PDT 2011


Ok, this seems to work for member functions (it returns false for 
non-functions, but I think that's fixable by just detecting "function vs not 
function" and then using the appropriate __traits(compiles)).

enum check = __traits(compiles, {
    alias ParameterTypeTuple!(__traits(getMember, Foo, name)) T;
    T t;
    __traits(getMember, foo, name)( t );
 });

I have not tested it on fancy arguments like ref/in/out/lazy/etc., or on 
functions with a return value. But it seems to work on void(void) and 
void(int).

Here's the full relevent code:

template check(string name)
{
    enum check = __traits(compiles, {
        alias ParameterTypeTuple!(__traits(getMember, Foo, name)) T;
        T t;
        __traits(getMember, foo, name)( t );
    });
}

// Any way to do this with foreach? Didn't work when I tried.
template checkMembers(members...)
{
    static if(members.length > 0)
    {
        // Check the first
        pragma(msg,
            members[0] ~ ": " ~
            (check!(members[0]) ? "true" : "false")
        );

        // Check the rest
        static if(members.length > 1)
            alias checkMembers!(members[1..$]) dummy;
    }
}

alias checkMembers!(__traits(allMembers, Foo)) dummy;




More information about the Digitalmars-d-learn mailing list