KeyType, ValueType traits for hashes
Mantis
mail.mantis.88 at gmail.com
Tue Jan 24 09:24:06 PST 2012
24.01.2012 8:43, Andrej Mitrovic пишет:
> But I did implement them poorly, this is better:
>
> 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;
> }
> }
>
Might I suggest that it be implemented this way:
template KeyType(AA)
{
static if (is(AA V : V[K], K))
{
alias K KeyType;
}
else static assert( 0, "Not associative array: " ~ AA.stringof );
}
Of course, most likely that user already did type check, but if not,
this will give less cryptic error:
Error: static assert "Not associative array: int"
instantiated from here: KeyType!(int)
, instead of:
Error: template instance KeyType!(int) KeyType!(int) does not match
template declaration KeyType(AA) if (isAssociativeArray!(AA))
More information about the Digitalmars-d-learn
mailing list