24.01.2012 20:49, bearophile пишет:
Mantis:
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))
It's a tradeoff. Your custom error message is more readable, but the failure at
the template constraint causes a error line at the instantiation point. Sadly I
think there is no solution that solves both problems (we have stack traces for
templates, but...).
Bye,
bearophile
True. Maybe something is possible to achieve in this direction?:
template verboseFail(alias Cond, string message)
{
static if (Cond)
{
enum verboseFail = true;
}
else
{
pragma( msg, message );
enum verboseFail = false;
}
}
template KeyType(AA)
if (verboseFail!(isAssociativeArray!AA, "Not assosiative array: " ~
AA.stringof))
{
static if (is(AA V : V[K], K))
{
alias K KeyType;
}
}
It doesn't work very nicely (the message is logged, but dos not look as
it is related to error), but perharps it may be improved somehow?