Class Array in D?

Jeroen Bollen jbinero at gmail.com
Wed Nov 20 10:31:36 PST 2013


On Wednesday, 20 November 2013 at 18:23:37 UTC, Ali Çehreli wrote:
> On 11/20/2013 10:12 AM, Jeroen Bollen wrote:
>
> > is there a way I can pass a TypeTulip to a function?
>
> alias TypeTulip = TypeTuple;
>
> ;)
>
> > Something like:
> >
> > Can I create a class array in D? Something like:
> >
> > interface A {}
> > class AA: A {}
> > class AB: A {}
> > class AC: A {}
> >
> > ClassList!A list = new ClassList!A {AA, AB, AC};
> >
> > void testf(ulong testv) {
> >      A a = new list[testv];
> > }
> >
> > I know about TypeTuple but that doesn't allow setting a
> requirement does
> > it?
>
> import std.stdio;
> import std.typetuple;
>
> interface A {}
> class AA: A {}
> class AB: A {}
> class AC: A {}
>
> alias TypeTulip = TypeTuple;
> alias list = TypeTuple!(AA, AB, AC);
>
> void testf(ulong testv) {
>     // NOTE: This is a compile-time foreach
>     foreach (i, Type; list) {
>         if (i == testv) {
>             A a = new Type();
>             writefln("I made it: %s", a);
>         }
>     }
> }
>
> void main()
> {
>     testf(1);
> }
>
> Note that the foreach loop above is not a loop that gets 
> executed at run time. Its body is expanded inline as code 
> multiple times as needed. I try to explain this a little under 
> the "Compile-time foreach" and "foreach with TypeTuple" 
> sections here:
>
>   http://ddili.org/ders/d.en/tuples.html
>
> Ali

That doesn't allow specifying a base class all members should be 
a part of though, or does it?


More information about the Digitalmars-d-learn mailing list