Expressing range constraints in CNF form

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 10 17:28:58 PDT 2017


https://github.com/dlang/phobos/pull/5461

There's many good advantages to this. The immediate one is the 
constraint is better structured and easier to understand. Then, the 
compiler can print the exact clause that failed, which improves the 
precision and quality of the error message.

We consider adding in the future some capability of printing 
user-defined error messages, one per clause. For example:

// Current
enum bool isInputRange(R) =
     is(typeof((ref R r) => r))
     && is(ReturnType!((R r) => r.empty) == bool)
     && is(typeof(lvalueOf!R.front))
     && is(typeof(lvalueOf!R.popFront));

// Possible (which change to language)
enum bool isInputRange(R) =
     is(typeof((ref R r) => r)) && "must be copyable"
     && is(ReturnType!((R r) => r.empty) == bool) && "must support bool 
empty"
     && is(typeof(lvalueOf!R.front)) && "must support front"
     && is(typeof(lvalueOf!R.popFront)) && "must support back";

// Also possible (no change to the language)
enum bool isInputRange(R) =
     is(typeof((ref R r) => r)) && msg("must be copyable")
     && is(ReturnType!((R r) => r.empty) == bool) && msg("must support 
bool empty")
     && is(typeof(lvalueOf!R.front)) && msg("must support front")
     && is(typeof(lvalueOf!R.popFront)) && msg("must support back");


Andrei


More information about the Digitalmars-d mailing list