Better error messages - from reddit

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Mar 4 15:47:12 UTC 2019


On Mon, Mar 04, 2019 at 02:50:11PM +0000, Adam D. Ruppe via Digitalmars-d wrote:
[...]
> How many template constraint messages would be nice if it just said
> "missing popFront" instead of 40 lines of overload failed spam?
[...]

This is why I'm starting to think sig constraints are not the genius
idea they first appeared to be. The basic problem is that when you write
a sig constraint, you're basically saying "I only accept template
arguments if they don't cause an error, otherwise it's not my problem
and somebody else can pick up the tab", whereas the essence of
user-friendly error-reporting is "I accept everything that looks like it
ought to work, and if something breaks, I'll tell you why it didn't
work".

With the former, you're essentially absolving yourself from the
responsibility of dealing with bad input by offloading it to other
overloads in the overload set. If nobody else in the overload set picks
up the tab, the compiler is left to clean up the mess, which it can't
possibly do a good job of because it wasn't hired to do this. I.e., it
doesn't understand the logical context of the error well enough to be
able to pinpoint and describe the problem in a user-friendly way.

To have user-friendly error reporting, the code that was intended to
pick up those bad arguments *need* to pick them up, and then report any
errors that were found -- which can then be made friendly because now
we're in the intended code context, and thereby have enough information
to emit a sensible message that makes sense in the intended context.

This is why years ago I said both here and in the Phobos PR queue that
sig constraints need to be written in such a way that anything that
*might* possibly be intended for that function ought to be accepted, and
static ifs should be used inside the function body to dispatch to the
code that handles the corresponding template arguments, with an
assert(0) at the end that tells you why the arguments weren't matched. A
human needs to write this, because auto-generated messages by the
compiler will never come up to the standard of user-friendliness.

This isn't the full solution, but it's at least a first stab at fixing
this problem. Ultimately, the philosophy behind sig constraints is
fundamentally incompatible with user-friendly error reporting, and
possibly a different approach needs to be adopted.


T

-- 
Study gravitation, it's a field with a lot of potential.


More information about the Digitalmars-d mailing list