float init 0 request
Forum User
forumuser at example.com
Wed Aug 19 20:24:11 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;
> ```
Regardless of how the value `f` is initialzed, this style
has a problem: The initial value of `f` is discarded. I think
that discarding values shall be avoided.
Thus I write the initialization:
```d
float f = (...) ? 1 : 2;
```
Compiler warnings/errors like "Defined but not used" are known for
variables. Is there a similar facility for values?
More information about the Digitalmars-d
mailing list