Class Array in D?

Ali Çehreli acehreli at yahoo.com
Wed Nov 20 10:23:36 PST 2013


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



More information about the Digitalmars-d-learn mailing list