Reflection: is type an inner class

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Oct 20 20:39:54 PDT 2012


On 10/21/12, Tyler Jameson Little <beatgammit at gmail.com> wrote:
> Say I have something like this:
>
>      class A {
>          class B {
>          }
>
>          B b;
>      }

I can't find a way to figure out if the inner type is static or not.
If it's static you don't need the outer class to instantiate it.
Figuring out if it's nested or not is doable:

class A
{
    class B { }
    B b;
}

template GetType(T)
{
    alias T GetType;
}

template GetParentType(alias T)
{
    alias GetType!(__traits(parent, T)) GetParentType;
}

template isInnerClass(T)
{
    enum bool isInnerClass = is(GetParentType!T == class);
}

void main()
{
    A.B ab;
    static assert(isInnerClass!(typeof(ab)));
}

(P.S. to others, why is __traits so impossible to work with? typeof
can't be used to extract the type, I had to write a special template
just to extract the type of a symbol..)


More information about the Digitalmars-d-learn mailing list