Runtime templates

John C johnch_atms at hotmail.com
Sat May 13 07:56:20 PDT 2006


Any ideas how to do the following at runtime with local variables?

enum Type { Void, Int, String }

template getType(Type value) {
   static if (value == Type.Int)
     alias int getType;
   else static if (value == Type.String)
     alias char[] getType;
   else
     alias void getType;
}

template makeDelegate(Type R, Type T1 = Type.Void, Type T2 = Type.Void) {
   static if (T2 != Type.Void)
     alias getType!(R) delegate(getType!(T1), getType!(T2)) makeDelegate;
   else static if (T1 != Type.Void)
     alias getType!(R) delegate(getType!(T1)) makeDelegate;
   else
     alias getType!(R) delegate() makeDelegate;
}

void main() {
   Type retType = /* call some function that returns a Type /*;
   Type paramType1 = /* ditto */;
   alias makeDelegate!(retType, paramType1) TypedDelegate;
}

Of course, this won't work because you can't use local variables as 
template arguments. Any possible workarounds, apart from passing 
compile-time constants?

John.



More information about the Digitalmars-d-learn mailing list