float init 0 request
pete
email at email.com
Tue Aug 18 07:16:40 UTC 2026
On Tuesday, 18 August 2026 at 03:25:37 UTC, Walter Bright wrote:
> On 8/17/2026 12:47 AM, pete wrote:
>> There is something I don't understand here. If an
>> uninitialised float is an error then why does the compiler not
>> just raise it at compile time rather than setting it to a
>> value that is always wrong and letting someone *hopefully*
>> find it at runtime?
>
> Because of code like this:
>
> ```d
> float f;
> if (...)
> f = 1;
> else
> f = 2;
> ```
>
> If the compiler gave an error for no initializer, the code will
> look like:
>
>
> ```d
> float f = 0; // shut up vacuous diagnostic message
> if (...)
> f = 1;
> else
> f = 2;
> ```
>
> which is inelegant.
>
> Real cases of this tend to be more complex, which leaves the
> code reviewer with the task of determining why f is explicitly
> set to 0 when 0 is not the right value for f.
I agree this looks a bit inelegant in this example. For more
complex code I wouldn't mind at all manually setting f to nan if
the compiler could not determine whether all flow paths resulted
in f being set. In this case I would expect a code reviewer to
also have an issue with it and manually setting it to nan would
highlight that there could be a problem.
I wouldn't really mind the nan default as long as there was a way
of opting out. I would rather the compiler not use poison values
if it can just easily ask me how to fix it during the compilation
phase. Some people like poison values. Maybe for them the
debugging for nan is trivial and that is fine. For some
applications it is a pain and the main problem is that sometimes
it doesn't even show up until much later.
More information about the Digitalmars-d
mailing list