Reflection: is type an inner class

Tyler Jameson Little beatgammit at gmail.com
Sat Oct 20 20:52:53 PDT 2012


On Sunday, 21 October 2012 at 03:40:15 UTC, Andrej Mitrovic wrote:
> 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..)

Hmm, maybe something like this should go into std.traits? This 
seems more readable than my hacky solution (__traits(compiles, 
A.B)).


More information about the Digitalmars-d-learn mailing list