float init 0 request
Dukc
ajieskola at gmail.com
Mon Aug 17 17:16:36 UTC 2026
On Monday, 17 August 2026 at 14:03:46 UTC, Quirin Schroll wrote:
> On Friday, 14 August 2026 at 06:38:02 UTC, Walter Bright wrote:
>> Floats are default initialized to NaN so people will be
>> coerced to explicitly code what they want the initialized
>> value to be.
>
> Maybe the next D Edition could require initialization of types
> that don’t initialize with a value equal to `0`. D’s behavior
> with these is annoying, because leads to needless debug
> sessions, and also surprising because no other language that D
> is similar to does that: C++. C# and Java all
> default-initialize to `0`. Essentially, character and
> floating-point types should behave like structs with `@disable
> this()`.
When done well (like in Haskell or Rust) I also think the best
default values are no default values.
But that requires it's easy to avoid declaring variables before
there are valid values for them. IMO it isn't the case in D. The
language is imperative at its root so there are often cases where
you want to declare a variable before initialising it. It is
almost always possible to avoid doing so if you're dogged about
it, but the cure tends to get worse at times than the disease.
D-style "paranoid" default values are the best option IMO - at
least for mundane types - when language ergonomics would suffer
without them. If, with help of future language features, we can
comprehensively avoid relying the default values some day without
kludgy tricks, then it's time to do what you suggest.
More information about the Digitalmars-d
mailing list