Now that's a DIP that could use some love

Meta jared771 at gmail.com
Fri Sep 18 15:43:44 UTC 2020


On Sunday, 13 September 2020 at 22:29:00 UTC, Andrei Alexandrescu 
wrote:
> https://github.com/dlang/DIPs/pull/131
>
> In essence:
>
> * Allow templates to accept multiple constraints.
> * Each constraint accepts the syntax "if (expr1, expr2)" where 
> expr2 is a string describing the requirement.
> * The string corresponding to the constraint that fails is 
> printed in the error message if no match is found.

If we're willing to rewrite most of the template constraints in 
Phobos, this might be workable:

enum string failConstraint(string name, string msg, Args...) = 
`alias %s = AliasSeq!(false, %s)`.format(Args[0], 
processArgs!(Args[1..$])); //processArgs is a helper template 
that converts types to string, etc...

template isInputRange(R)
{
     static if (!is(typeof(R.init) RInit == R))
         mixin(failConstraint!(isInputRange, `%s.init must have 
type %s, but it has type %s`, R, R, RInit);
     else static if (!is(ReturnType!((R r) => r.empty) REmpty == 
bool))
         mixin(failConstraint!(isInputRange, `ReturnType!((%s r) 
=> r.empty) must be of type bool, but it is %s`, R, REmpty);
     else static if (...)
         ...etc.
     else
         enum isInputRange = true;
}

As long as constraints are able to be written in this form and 
the proposed `if (expr, msg)` constraint syntax allows tuples to 
be expanded over it, just like with functions.


More information about the Digitalmars-d mailing list