Skip to content

Instantly share code, notes, and snippets.

@wilkie
Forked from anonymous/gist:426965
Created June 5, 2010 20:42
Show Gist options
  • Save wilkie/426976 to your computer and use it in GitHub Desktop.
Save wilkie/426976 to your computer and use it in GitHub Desktop.
Getting a tuple of inherited classes with templates in D.
class A {
int bar;
}
class B : A {
int foo;
}
class C : B {
}
template Tuple(T...) {
alias T Tuple;
}
template BaseClassOf(T) {
static if (is(T S == super)) {
alias S[0] BaseClassOf;
}
else {
static assert(false, "fail");
}
}
// Gives list of base classes including the class itself and Object
template BaseClassListOf(T) {
static if (is(T == Object)) {
alias Object BaseClassListOf;
}
else {
alias Tuple!(T, BaseClassListOf!(BaseClassOf!(T))) BaseClassListOf;
}
}
pragma(msg, BaseClassListOf!(C));
pragma(msg, BaseClassListOf!(C)[0]);
pragma(msg, BaseClassListOf!(C)[1].tupleof);
pragma(msg, BaseClassListOf!(C)[2].tupleof);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment