Doctor J wrote:
I'd like to make a compile-time constant associative array mapping a list of
types to an integer:
int[??] typemap;
typemap[(int,float)] = 1;
typemap[(long,double)] = 2;
...
int t = typemap[(int,float)];
I tried playing with std.typetuple but no luck. I can get halfway there with a
function template, but then I can't iterate over the keys or values. Is there
a way to do something like this in D? More generally, is there a way to store
a type in a variable?
Just curious, how did you do this function template thing?
Thanks.
Types are a compiletime only thing. To get a "runtime" handle for a
type, use TypeInfo. For each type, there exists exactly one TypeInfo
object instance. You can get this object with typeid():
TypeInfo ti = typeid(int);
Your example above could actually be implemented like this:
int[TypeInfo[]] typemap;
typemap[[typeid(int), typeid(float)]] = 1;
(Two [] bracket pairs because one is for indexing into the AA, and one
is an array delegate for constructing the TypeInfo[] array, which is
used as key.)