Producing nicer template errors in D libraries

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Apr 10 14:08:38 PDT 2012


On Tue, Apr 10, 2012 at 10:13:29PM +0200, Artur Skawina wrote:
[...]
>    // Give the user at least a chance to figure out what's wrong when a template
>    // constraint doesn't match - print both the found and the expected types.
>    bool _ttmm(T, E)() {
>       pragma(msg, "\nExpected: '" ~ E.stringof ~"';\n   found: '" ~ T.stringof ~"'.\n");
>       return 0;
>    }
> 
> static assert wasn't necessary.
[...]

I like this idea too. In fact, it might be better than having a catchall
overload with a static assert, because here you can actually see what
constraints were tried and failed for each failed sig constraint. May
make bugs easier to locate if the relevant sig constraints are very
complex. (Plus, in the latter case, it's not so easy to write a
catch-all constraint due to D's (deliberate?) lack of a default-matching
mechanism for templates.)

The only complaint, though, is, wouldn't this produce lots of noise even
with successful compiles? I assume the pragma will run even if there's a
later template match after a series of failed overloads.


T

-- 
Ignorance is bliss... but only until you suffer the consequences!


More information about the Digitalmars-d mailing list