Simple and effective approaches to constraint error messages

Marc Schütz via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 26 04:26:45 PDT 2016


On Monday, 25 April 2016 at 17:52:58 UTC, Andrei Alexandrescu 
wrote:
> Idea #1: Detect and use CNF, print which clause failed
> ====
>

I prefer this one, because it should work without modifying 
library or user code.

> Idea #2: Allow custom error messages
> ====
>
> The basic idea here is to define pragma(err, "message") as an 
> expression that formats "message" as an error and returns 
> false. Then we can write:
>
> R find(R, E)(R range, E elem)
> if ((isInputRange!R || pragma(err, R.stringof~" must be an 
> input range")
>    &&
>    (is(typeof(range == elem) == bool) || pragma(err, "..."))
>

Currently, there is no boolean short-cut evaluation in template 
constraints, see:

     bool foo()() {
         pragma(msg, "foo");
         return true;
     }

     bool bar()() {
         pragma(msg, "bar");
         return true;
     }

     void main() {
         static assert(__traits(compiles, foo() || bar()));
     }

Prints "foo" and "bar", even though bar() wouldn't need to be 
evaluated anymore after foo() returned true.


More information about the Digitalmars-d mailing list