Poor error messages and a quiz
bearophile
bearophileHUGS at lycos.com
Mon Feb 25 16:28:04 PST 2013
Two notes born from a Rosettacode entry
(http://rosettacode.org/wiki/Align_columns#D ), but general to
deserve their own post.
This program has a bug:
import std.stdio, std.string, std.algorithm, std.range;
void main() {
auto data =
"Given$a$txt$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
column$are$separated$by$at$least$one$space.
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column."
.splitLines.map!q{ a.chomp("$").split("$") };
int[int] maxWidths;
foreach (line; data)
foreach (i, word; line)
maxWidths[i] = max(maxWidths.get(i, 0), word.length);
foreach (just; [&leftJustify!string, ¢er!string,
&rightJustify!string])
foreach (line; data)
writefln("%-(%s %)", iota(line.length)
.map!(i => just(word, maxWidths[i], ' ')));
}
DMD gives just the error message:
test1.d(21): Error: no property 'map' for type 'Result'
Can you spot the problem?
Lately I have started to omit the () in UCFS (according to this:
http://article.gmane.org/gmane.comp.lang.d.phobos/7222 ) and I am
seeing several similar situations with insufficient error
messages.
Is it possible for DMD to improve its error messages in similar
situations?
- - - - - - - - - - - - - - - - -
This program is derived from another buggy version of same code:
import std.stdio, std.algorithm, std.typetuple, std.range;
void main() {
auto items = [[10, 20], [30]];
foreach (_; 0 .. 2) {
foreach (sub; items) {
iota(sub.length)
.map!((i){ writeln(sub); return 0; })
.array();
}
}
writeln();
foreach (_; TypeTuple!(0, 1)) {
foreach (sub; items) {
iota(sub.length)
.map!((i){ writeln(sub); return 0; })
.array();
}
}
}
Its output:
[10, 20]
[10, 20]
[30]
[10, 20]
[10, 20]
[30]
[10, 20]
[10, 20]
[30]
[30]
[30]
[30]
Can you guess why the output of the two foreach(_) is different?
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list