F i L wrote:
Walter Bright wrote:
It catches only a subset of these at compile time. I can craft
any number of ways of getting it to miss diagnosing it.
Consider this one:
float z;
if (condition1)
z = 5;
... lotsa code ...
if (condition2)
z++;
[...]
Yes, but that's not really an issue since the compiler informs
the coder of it's limitation. You're simply forced to
initialize the variable in this situation.
I just want to clarify something here. In C#, only class/struct
fields are defaulted to a usable value. Locals have to be
explicitly set before they're used.. so, expanding on your
example above:
float z;
if (condition1)
z = 5;
else
z = 6; // 'else' required
... lotsa code ...
if (condition2)
z++;
On the first condition, without an 'else z = ...', or if the
condition was removed at a later time, then you'll get a compiler
error and be forced to explicitly assign 'z' somewhere above
using it. So C# and D work in "similar" ways in this respect
except that C# catches these issues at compile-time, whereas in D
you need to:
1. run the program
2. get bad result
3. hunt down bug
NaNs in C# are "mostly" (citations needed) set to ensure fields
are initialized in a constructor:
class Foo
{
float f = float.NaN; // Can't 'f' use unless Foo is
// properly constructed.
}