[Issue 3849] Compiler should catch incomplete initialisation of an array
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue May 28 04:45:28 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3849
--- Comment #20 from bearophile_hugs at eml.cc 2013-05-28 04:45:25 PDT ---
A small example why enforcing array lengths match improves safety of D
programs. This part of a program uses strings to define a binary decision
table, but it's easy to make mistakes in the strings:
struct DataPair { string message, truth; }
immutable DataPair[] solutions = [
{"Check the power cable", "..#....."},
{"Check the printer-computer cable", "#.#....."},
{"Ensure printer software is installed", "#.#.#.#."},
{"Check/replace ink", "##..##.."},
{"Check for paper jam", ".#.#...."}];
An improvement is to use fixed-sized arrays so the compiler catches some bugs
at compile-time:
struct DataPair(uint N) {
string message;
immutable(char)[N] truth;
}
immutable DataPair!8[] solutions = [
{"Check the power cable", "..#....."},
{"Check the printer-computer cable", "#.#....."},
{"Ensure printer software is installed", "#.#.#.#."},
{"Check/replace ink", "##..##.."},
{"Check for paper jam", ".#.#...."}];
But currently the compiler only gives an error if you add one more char:
{"Check the power cable", "..#......"},
And not if you miss one:
{"Check the power cable", "..#...."},
And this is not a nice solution also because most D programmers don's write
code like this:
immutable DataPair!8[] solutions = [
{"Check the power cable", "..#.....".fixed},
{"Check the printer-computer cable", "#.#.....".fixed},
{"Ensure printer software is installed", "#.#.#.#.".fixed},
{"Check/replace ink", "##..##..".fixed},
{"Check for paper jam", ".#.#....".fixed}];
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list