-
-
Save wilkie/426976 to your computer and use it in GitHub Desktop.
Getting a tuple of inherited classes with templates in D.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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