Fixing C's Biggest Mistake

Max Samukha maxsamukha at gmail.com
Tue Dec 27 09:41:59 UTC 2022


On Tuesday, 27 December 2022 at 00:38:33 UTC, Walter Bright wrote:
> C uninitialized variables was another fountain of endless and 
> hard to track down problems. D initializes them by default for 
> a very good reason.

Is it initialization or "branding"? Honestly, I've never been 
comfortable with D's initialization semantics.

On the one hand, the language postulates that, for any type T, 
there is always a valid value T.init. On the other hand, it makes 
that weird distinction between initialization and "branding". 
That has always felt a bit schizophrenic.

If T.init is supposed to be a valid value, then the constructor 
receives an already initialized object (not some "branded" 
abberant), so the constructor actually plays the role of 
assignment.

If T.init is supposed to be an invalid value useful for 
debugging, then variables initialized to that value... are not 
initialized.

IMO, D's attempt to conflate those two meanings of T.init is a 
failed experiment. Even C++ seems to be going in a right 
direction (https://youtu.be/ELeZAKCN4tY?t=4887) in that respect.


More information about the Digitalmars-d mailing list