Floating point types default to NaN?

Adam D. Ruppe destructionator at gmail.com
Sat Nov 25 22:13:43 UTC 2017


On Saturday, 25 November 2017 at 16:16:52 UTC, A Guy With a 
Question wrote:
> If D chooses it's defaults to make errors stick out, why not 
> just error at declaration if they don't explicitly set it to 
> something.

It technically did:

https://dlang.org/spec/function.html#local-variables

"It is an error to use a local variable without first assigning 
it a value. The implementation may not always be able to detect 
these cases. Other language compilers sometimes issue a warning 
for this, but since it is always a bug, it should be an error. "


But that paragraph was never implemented (it is a bit easier said 
than done to actually detect given if conditions and stuff too, 
especially in the early days of D when the compiler didn't even 
make any effort to track such things, though it does now...). The 
compiler author took the easy way out of initializing them to 
invalid values instead.

While it is more realistic to implement technically now than it 
was years ago when the current behavior got in, I think we're at 
the point now that so many people use it as a convenience thing 
on ints and nulls that there'd be hell to pay if the compiler 
actually started calling it an error :(



More information about the Digitalmars-d-learn mailing list