[Issue 10757] New: int incremented with double NaN doesn't give a "cannot implicitly convert expression" error
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Aug 4 11:00:27 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10757
Summary: int incremented with double NaN doesn't give a "cannot
implicitly convert expression" error
Product: D
Version: D2
Platform: x86
OS/Version: Windows
Status: NEW
Keywords: accepts-invalid
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2013-08-04 11:00:25 PDT ---
With dmd 2.064alpha this terrible program compiles with no errors nor warnings,
and it runs with no asserts:
void main() {
double x;
int y;
y += x;
assert(y == int.min);
y += 3.5;
assert(y == -2147483644);
}
What I'd like: that code to become a compile time error (a "cannot implicitly
convert expression" error), because I think it's against the idea of strong
typing. I prefer a language that doesn't silently drops floating point parts
(like 0.5 here) and that catches me if I increment an integer variable by
mistake by a floating point value.
(Probably I will not close down this bug report, so if someone (like Walter) is
against it then he should close it down. Such person should also show one or
more practical situations where allowing that code gives an advantage over
generating a compile-time error. I leave the burden of showing the usefulness
on the shoulders of the person willing to close this issue down.)
In a dynamically typed language as Python2.6 int+float or int+=float return a
float, so this behavour is acceptable:
>>> x = float("nan")
>>> y = 0
>>> y += x
>>> y
nan
I am aware that in D the increment at line 4 is accepted, but this is just a
matter of disabling the value range tests that are so good in D, this is a
different situation from adding a double to an int:
void main() {
int x;
byte y;
y += x; // line 4, OK
byte z = x; // Error: cannot implicitly convert
}
--
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