non-lambda overloads for lambda-only things
Ali Çehreli
acehreli at yahoo.com
Fri Apr 16 00:39:24 UTC 2021
On 4/15/21 3:02 PM, Steven Schveighoffer wrote:
> Have you ever written something like:
>
> auto str = "hello".map(r => r.toUpper).array;
> 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! :)
Coincidentally, I have wasted considerable amount of time just now with
the following error message:
import std.format;
import std.algorithm;
void main() {
auto content = q"EOS
1 first line
2 second line
EOS";
foreach (line; content) {
int i;
string s;
line.formattedRead!"%s %s"(i, s);
}
}
The first line of the 78-line error message is this:
/usr/include/dmd/phobos/std/format.d(1492): Error: template
`std.range.primitives.empty` cannot deduce function from argument types
`!()(immutable(char))`, candidates are:
but the line in format.d, which my editor was showing me was this:
assert(!r.empty, "Required at least one more input");
I admit that my brain was reading the error message on the source code
instead of the actual error message and I was scratching my head, trying
to understand what "one more input" meant.
The programmer's error was something completely different: 'content' is
not an iterable thing; it's a string. Argh! So, one solution is to
iterate over content.splitter('\n') in the loop.
Ali
More information about the Digitalmars-d
mailing list