Producing nicer template errors in D libraries

Artur Skawina art.08.09 at gmail.com
Tue Apr 10 14:48:06 PDT 2012


On 04/10/12 23:08, H. S. Teoh wrote:
> 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.

Yes - this isn't a problem in my case because the templates are also specialized
for each signal. So it only has to deal with trying to register a signal
with a wrong callback signature; something that can be surprisingly hard to figure
out as the compiler won't tell you what it didn't like about the arguments.
static assert wasn't necessary - in this case.

artur


More information about the Digitalmars-d mailing list