foreach of classes

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 12 05:09:51 PDT 2016


On 4/9/16 6:10 AM, Lucien wrote:
> Hello.
>
> When I do:
>
> ---------------------
> class MyClass{..}
> class YourClass{..}
> class OurClass{..}
>
> YourClass yc = new YourClass();
>
> foreach (auto id; [ typeid(MyClass), typeid(YourClass), typeid(OurClass) ])
> {
>    if (typeid(yc) == id)
>    {
>      writeln("It works !");
>    }
> }
> ---------------------
> The compiler says: basic type expected, not auto
> Why can't I have one time the type id of MyClass, then of YourClass and
> of OurClass ?
> Is there an alternative ?

I know this is not exactly what you may be expecting, but you could do 
what you want this way:

import std.meta: AliasSeq;

foreach(classType; AliasSeq!(MyClass, YourClass, OurClass))
{
    // in here, classType is now an alias for the actual class
    auto id = typeid(classType); // if you want it in TypeInfo_Class form
}

This is called a static foreach.

-Steve


More information about the Digitalmars-d-learn mailing list