Thread overview
Finding Super class from Derived Class at compile time
Apr 15, 2019
LeqxLeqx
Apr 15, 2019
Ali Çehreli
Apr 15, 2019
Adam D. Ruppe
Apr 16, 2019
LeqxLeqx
April 15, 2019
Hello!

I have a question regarding attempting to access the super class of a derived class at compile time.

Specifically, if I have:

    class A { }
    class B : A { }

    void func(T)()
    {
      /+ find super-class of T +/
    }

    int main ()
    {
      func!B; /+ func would find A +/
      return 0;
    }

in `func(T)()' how would I (if it is possible) check what class T derives from? So `func!B' would be able to find `A'? I've looked through the __traits options but none of them seem to be able to do what I need. Unfortunately (for me anyways), __traits(parent, ...) returns the module, not the super class.

Is this at all possible (at compile time)?

Thank you
April 14, 2019
On 04/14/2019 10:06 PM, LeqxLeqx wrote:
> Hello!
> 
> I have a question regarding attempting to access the super class of a derived class at compile time.

BaseClassesTuple and friends:

  https://dlang.org/phobos/std_traits.html

Ali
April 15, 2019
On Monday, 15 April 2019 at 05:20:37 UTC, Ali Çehreli wrote:
> BaseClassesTuple and friends:
>
>   https://dlang.org/phobos/std_traits.html

And the implementation of that is the `is` expression

https://dlang.org/spec/expression.html#IsExpression

specifically, with the `super` keyword.

static if (is(YourClass ListOfBaseTypes == super))
   // use ListOfBaseTypes here; you can foreach, index, or slice it to get what you are looking for.
April 16, 2019
On Monday, 15 April 2019 at 22:45:59 UTC, Adam D. Ruppe wrote:
> On Monday, 15 April 2019 at 05:20:37 UTC, Ali Çehreli wrote:
>> BaseClassesTuple and friends:
>>
>>   https://dlang.org/phobos/std_traits.html
>
> And the implementation of that is the `is` expression
>
> https://dlang.org/spec/expression.html#IsExpression
>
> specifically, with the `super` keyword.
>
> static if (is(YourClass ListOfBaseTypes == super))
>    // use ListOfBaseTypes here; you can foreach, index, or slice it to get what you are looking for.

That's perfect. Thank you both so much!