Template constraints

Chris Williams via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 17 10:55:36 PST 2015


On Saturday, 14 February 2015 at 17:00:33 UTC, Andrei 
Alexandrescu wrote:
> There's been recurring discussion about failing constraints not 
> generating nice error messages.
>
> void fun(T)(T x) if (complicated_condition) { ... }
> struct Type(T)(T x) if (complicated_condition) { ... }
>
> If complicated_condition is not met, the symbol simply 
> disappears and the compiler error message just lists is as a 
> possible, but not viable, candidate.
>
> I think one simple step toward improving things is pushing the 
> condition in a static_assert inside type definitions:
>
> void fun(T)(T x) if (complicated_condition) { ... } // no change
> struct Type(T)(T x)
> {
>   static assert(complicated_condition, "Informative message.");
>   ...
> }
>
> This should improve error messages for types (only). The 
> rationale is that it's okay for types to refuse compilation 
> because types, unlike functions, don't overload. The major 
> reason for template constraints in functions is allowing for 
> good overloading.
>
>
> Andrei

This seems like a lot of manual labor to cover a compiler 
limitation that is going to affect not just Phobos, but every D 
project which uses constraints (or template type specialization?) 
instead of interfaces. Ideally, the compiler would be able to 
say, "Well the function name and the parameter list exactly match 
these possibilities, so let's run through each constraint applied 
in the set of possibilities and explicitly dump the boolean 
result for each constraint."


More information about the Digitalmars-d mailing list