User-defined template error messages

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jan 13 18:54:52 UTC 2019


We've had this problem for a long time - template doesn't match because 
of a complex constraint, the error message is unhelpful, and the user 
doesn't know what to do.

I think this is an important missed opportunity. One of the promises of 
Design by Introspection is it raises the level of error messages from 
mere built-in canned messages to higher-level messages expressed in 
terms of problem-space entities.

A simple way to address this would be with a two-pronged proposal:

1. Allow multiple "if" constraints in a template. All "if" constraints 
must match. This means a natural way to write complex constraints is a 
conjunction of simpler constraints.

2. Extend the syntax to:

if (condition) else string

The string (which crucially can be a CT expression involving template 
parameters) is the error message that would be displayed should the 
condition be false.

When an overload set is being looked up, if no match then the string 
corresponding to the first failed condition in each overload is printed 
as the error message.

Example from std. Current sig:

InputRange2 moveAll(InputRange1, InputRange2)(InputRange1 src, 
InputRange2 tgt)
if (isInputRange!InputRange1 && isInputRange!InputRange2
         && is(typeof(move(src.front, tgt.front))));

With the proposal:

InputRange2 moveAll(InputRange1, InputRange2)(InputRange1 src, 
InputRange2 tgt)
if (isInputRange!InputRange1)
else InputRange1.stringof ~ " must be an input range"
if (isInputRange!InputRange2)
else InputRange2.stringof ~ " must be an input range"
if (is(typeof(move(src.front, tgt.front))))
else "Cannot move from the front of "~InputRange1.stringof~
     " to the front of "~InputRange2.stringof;

Larger but offers the benefits of beautiful error messages.


Andrei


More information about the Digitalmars-d mailing list