[Issue 3847] New: To avoid a C code bug
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Feb 23 18:11:53 PST 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3847
Summary: To avoid a C code bug
Product: D
Version: 2.040
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-02-23 18:11:52 PST ---
What does this D2 program print, and why?
import std.stdio;
bool thirdElementIsThree(int[] a) {
return a.length >= 3 & a[2] == 3;
}
void main() {
int[][] tests = [[6, 5, 4, 3, 2, 1],
[1, 2],
[1, 2, 3],
[1, 2, 3, 4 ],
[1]];
int n = 0;
try {
int i = 0;
while (true) {
if (thirdElementIsThree(tests[i++]))
n++;
}
} catch(Error e) {
// No more tests to process
}
writeln(n); // prints?
}
Using 'and' and 'or' instead of && and || helps reduce some possible programmer
mistakes, because the programmer may write & or | and the compiler may not
catch the mistake. Using those keyword (as in Python and many other languages)
avoids such mistakes, and C programmer will not be confused by this change
because the || and && becomes deprecated, as the 'l' suffix for numbers. 'and'
and 'or' are also quite less cryptic than && and || symbols.
&& and || can be kept to keep C compatibility, but their usage can be
discouraged in new normal D2 code.
In real code it happens to write a & where you want to write &&, this is not an
uncommon bug.
--
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