Bug 7356 – Implement KeyType, ValueType for hashes in std.traits
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-23T22:50:00Z
Last change time
2012-04-19T18:57:25Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2012-01-23T22:50:34Z
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).
Comment #1 by k.hara.pg — 2012-01-24T06:05:29Z
I think the type-deduction template names should be 'KeyTypeOf' and 'ValueTypeOf'.
It is consistent with FunctionTypeOf and StringTypeOf (it's undocumented).
Comment #2 by andrej.mitrovich — 2012-01-24T06:38:05Z
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.
Comment #3 by issues.dlang — 2012-01-24T08:05:06Z
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.
Comment #4 by bearophile_hugs — 2012-01-24T10:42:57Z
KeyType and ValueType are good enough names, I used the same in my code.
Comment #5 by k.hara.pg — 2012-01-26T04:07:44Z
(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.
Comment #6 by andrej.mitrovich — 2012-04-19T18:57:25Z