Class Array in D?

Adam D. Ruppe destructionator at gmail.com
Wed Nov 20 10:45:48 PST 2013


On Wednesday, 20 November 2013 at 18:31:37 UTC, Jeroen Bollen 
wrote:
> That doesn't allow specifying a base class all members should 
> be a part of though, or does it?

You could write your own tuple template for it, or just let the 
compile fail on the factory function: the A a = new T(); will 
fail if A isn't a base class or interface of T.

Putting the check in the list could look like this:

bool checkClassList(Base, T...)() {
    foreach(t; T) {
      static if(!is(t : Base))
          static assert(0, t.stringof ~ " is not a child of " ~ 
Base.stringof);
    }
    return true;
}

template ClassList(Base, T...) if(checkClassList!(Base, T)) {
     alias ClassList = T;
}


Usage:

alias list = ClassList!(A, AA, AB, AC); // good


add:

class B {}
alias list = ClassList!(A, AA, AB, AC, B);

and get error:

test50.d(12): Error: static assert  "B is not a child of A"
test50.d(19):        instantiated from here: checkClassList!(A, 
AA, AB, AC, B)


More information about the Digitalmars-d-learn mailing list