Indexing an associative array with a list of types
    Doctor J 
    nobody at nowhere.com
       
    Sat Apr 11 15:13:54 PDT 2009
    
    
  
grauzone Wrote:
> Just curious, how did you do this function template thing?
    static int typesToInt(T1, T2) ()
    {
        static if (is (T1 == int) && is (T2 == float))
            return 1;
        else if (is (T1 == long) && is (T2 == double))
            return 2;
        else
            static assert (false, "Unrecognized types."); 
    }
Yuck.  :)
> 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.)
Sweet!  Exactly what I was looking for.  Thanks.
-John
    
    
More information about the Digitalmars-d-learn
mailing list