non-lambda overloads for lambda-only things

Jordan Wilson wilsonjord at gmail.com
Fri Apr 16 00:08:17 UTC 2021


On Thursday, 15 April 2021 at 22:02:10 UTC, Steven Schveighoffer 
wrote:
> Have you ever written something like:
>
> auto str = "hello".map(r => r.toUpper).array;
>
> and been confronted with fun errors like:
>
> Error: template std.algorithm.iteration.map cannot deduce 
> function from argument types !()(string, void), candidates are:
> /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(479):
>       map(fun...)
>   with fun = ()
>   must satisfy the following constraint:
>        fun.length >= 1
>
> What the hell is the "fun length"? This is not FUN!!!
>
> In any case, I thought of maybe adding something like this:
>
> void map()(...) {
>    static assert(false, "You didn't mean to do this. Make your 
> lambda a template parameter");
> }
>
> To instead get a better error message:
>
> Error: static assert:  "You didn't mean to do this. Make your 
> lambda a template parameter"
>
>  Is this worth adding to Phobos?
>
> -Steve

As a noob, when I see error messages like the first one, I 
immediately just pick out the line number, and just stare at the 
offending code until I realise I've missed an "!". I think your 
proposed error message tells me instantly what went wrong.

Thanks,

Jordan


More information about the Digitalmars-d mailing list