On 11/20/2013 08:35 AM, Jeroen Bollen wrote:
I was more looking for a way to just access a type/class by specifying an index... I don't really get your code.
TypeTuple can contain types. Do you know the index at compile time or at run time? Here is a program that demonstrates constructing an object for both of those cases:
module deneme; import std.stdio; import std.typetuple; import std.string; interface I {} class C1 : I {} class C2 : I {} // Question: Is there a way to determine the name of this module? string moduleName = "deneme"; static const typeNames = [ "C1", "C2" ]; void makeWithCompileTimeIndex(size_t index)() { alias typeList = TypeTuple!(C1, C2); alias Type = typeList[index]; auto o = new Type(); writefln("I made an object by using a compile-time index: %s", o); } void makeWithRunTimeIndex(size_t index) { string fullTypeName = format("%s.%s", moduleName, typeNames[index]); auto o = cast(I)Object.factory(fullTypeName); writefln("I made an object by using a run-time index: %s", o); } void main() { makeWithCompileTimeIndex!1(); writefln("I can make an object of these types: %s", typeNames); write("Please enter the index of the type that you want: "); size_t index; readf(" %s", &index); makeWithRunTimeIndex(index); } Ali