Now that's a DIP that could use some love

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Sep 14 15:28:29 UTC 2020


On 9/13/20 8:00 PM, Adam D. Ruppe wrote:
> On Sunday, 13 September 2020 at 22:29:00 UTC, Andrei Alexandrescu wrote:
>> Solves a long-standing, difficult problem of outputting meaningful, 
>> useful error messages.
> 
> Since this was written, the compiler's output has improved significantly.
> 
> ---
> void foo(T)() if(true && is(T == string)) {}
> void foo(T)() if(false && is(T == float)) {}
> 
> void main() { foo!int(); }
> ---
> 
> $ dmd cons
> cons.d(4): Error: template cons.foo cannot deduce function from argument 
> types
> (int)(), candidates are:
> cons.d(1):        foo(T)()
>    with T = int
>    must satisfy the following constraint:
>         is(T == string)
> cons.d(2):        foo(T)()
>    with T = int
>    must satisfy the following constraint:
>         false
> 
> 
> What benefit would it bring adding a string to it? Instead of `is(T == 
> string)` it would say "T must be a string"?

Yes, indeed the recent improvements are noticeable. On a synthetic 
example it looks all dandy, but clearly there's be a large improvement 
by allowing semantic error messages. I just happened to have 
std.algorithm.comparison opened and here are a few examples:

template among(values...)
if (isExpressionTuple!values, "All values in the among expression must 
be expressions")

auto max(T...)(T args)
if (T.length >= 2)
if (!is(CommonType!T == void), "Types "~T.stringof~" don't have a common 
type for choosing the maximum.")

(similar for min)

CommonType!(T, Ts) either(alias pred = a => a, T, Ts...)(T first, lazy 
Ts alternatives)
if (alternatives.length >= 1, "Need at least one alternative besides 
primary choice")
if (!is(CommonType!(T, Ts) == void), "Types "~AliasSeq!(T, 
Ts).stringof~" need a common type")
if (allSatisfy!(ifTestable, T, Ts), "Types "~AliasSeq!(T, Ts).stringof~" 
must be testable with the if (...) statement")

Better error messages are always a good investment.


More information about the Digitalmars-d mailing list