Identifying associative array types from a template?

Christopher Wright dhasenan at gmail.com
Sat Dec 1 17:41:06 PST 2007


Christopher Wright wrote:
> I want to identify the types that make up an associative array that I 
> get as a template parameter, something like:
> 
> template decomposeAA (T) {
>    static if (is (TT TVal : TVal[TKey])) {
>       alias Tuple!(TVal, TKey) decomposeAA;
>    } else {
>       static assert (false, "not an associative array");
>    }
> }
> 
> 
> Now, if I put conditions on TKey, this works, at least in D2. If I don't 
> put conditions on TKey, it always static asserts, even if I give it an 
> associative array.
> 
> Is this a bug? Is there another way to get the key type and value type?

Okay, for a workaround I can do:
alias typeof(T.keys[0]) TKey;
alias typeof(T[TKey.init]) TValue;

And that'll match primitive AA's and anything that offers a similar 
interface. Which is good enough for my purposes.

Still, I think the behavior of my initial attempt is wrong: it seems to 
follow the specs, and it's the naive approach, and it fails silently. It 
should at least have an error message.


More information about the Digitalmars-d-learn mailing list