non-lambda overloads for lambda-only things
Jon Degenhardt
jond at noreply.com
Fri Apr 16 20:53:40 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
Yes!! As others have said, this hit me when I first started
learning D. I just wasn't expecting to pass the lambda as a
template parameter. An error message that more quickly pointed to
what needed to be done would have removed a start-up barrier.
--Jon
More information about the Digitalmars-d
mailing list