Compile-type array of classes?

anonymous anonymous at example.com
Sun Feb 23 12:52:58 PST 2014


On Sunday, 23 February 2014 at 20:34:07 UTC, Gordon wrote:
> Hello,
>
> I'm sure this can be done, I'm just not sure what are the 
> correct terms to search for...
>
> Given one interface, and multiple implementation classes, I 
> want to create a list of the classes (in compile time).
> A contrived example:
> ===
> interface Animal
> {
>   static const string name() @property;
>   void make_noise();
> }
>
> class Dog : Animal
> {
>    static const string name() @property { return "dog" ; }
>    void make_noise() { writeln("Woof"); }
> }
>
> class Cat : Animal
> {
>    static const string name() @property { return "cat" ; }
>    void make_noise() { writeln("Meow"); }
> }
> ===
>
> What I want is:
>
> ===
> static const xxxxx[] available_animals = [ dog, cat ];
> ===
>
> and then:
>
> ===
> foreach (a; available_animals) {
>    writeln("We have a ", a.name);
> }
> ===
>
> Given that "name" is a static member function, it should be 
> doable.
> But I'm not sure about the correct syntax of defining 
> "available_animals".
>
> Any suggestions are welcomed,
>  -gordon

import std.typetuple: TypeTuple;
alias available_animals = TypeTuple!(Dog, Cat);


More information about the Digitalmars-d-learn mailing list