How to implement this?

Benjamin Thaut code at benjamin-thaut.de
Mon Jun 10 04:01:38 PDT 2013


Am 10/06/2013 11:42, schrieb Elvis:
> class A
> {
>      enum TypeID = 1;
> }
> class B : A
> {
>      enum TypeID = 2;
> }
>
> class C : A
> {
>      enum TypeID = 3;
> }
>
> class D : B
> {
>      enum TypeID = 4;
> }
>
> ...
>
>
> Could anybody shed some light on how to make these TypeIDs auto
> increment at compile time?
>
>

Thats not possible due to the compilation model. You can however do:

alias TypeTuple!(A, B, C, D) list;

class A
{
    enum TypeID = staticIndexOf!(typeof(this), list);
}

class B
{
    enum TypeID = staticIndexOf!(typeof(this), list);
}

Further you could write yoruself a helper template which calls 
staticIndexOf and does a static assert in case it returns -1 (type not 
found).

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d-learn mailing list