I've had a use-case for these but they were not in std.traits, so here's an implementation: import std.traits; template KeyType(AA) if (isAssociativeArray!AA) { static if (is(AA V : V[K], K)) { alias K KeyType; } } template ValueType(AA) if (isAssociativeArray!AA) { static if (is(AA V : V[U], U)) { alias V ValueType; } } If I get an OK I can make a pull for this (with documentation).
I think the type-deduction template names should be 'KeyTypeOf' and 'ValueTypeOf'. It is consistent with FunctionTypeOf and StringTypeOf (it's undocumented).
I've based it on ReturnType. There's a mix of these names, such as: FunctionTypeOf FieldTypeTuple Some have "Of", others don't. I don't see what "Of" adds, except verbosity.
I wouldn't expect the Of on them. It's awkward in comparison IMHO. I don't know why FunctionTypeOf and StringTypeOf have Of on them. But if Of is both used and not used (as appears to be the case), then I wouldn't use it - especially when the cases that _do_ use it are undocumented.
KeyType and ValueType are good enough names, I used the same in my code.
(In reply to comment #2) > Some have "Of", others don't. I don't see what "Of" adds, except verbosity. IMHO, it comes from the typeof() feature. First of all, and for fairness, `StringTypeOf` is the one that I added into Phobos in the past, so original XXXTypeOf is only FunctionTypeOf. I'm not a native English speaker, but it seems to me that XXXTypeOf!(Y) is more natural than XXXType!(Y). The former looks like a sentence, but latter like a noun. This kind of templates work like meta function, and function name usually contains verb. So I sometimes feel wrong about the latter. And, 'KeyType' and 'ValueType' are often used in user code. I think we should avoid using generic name as the piece of library, as far as possible.
https://github.com/D-Programming-Language/phobos/commit/b64751067f2364d33011e8de736c5f8de6a2f019