Default initialization no longer a thing

Julian Fondren julian.fondren at gmail.com
Tue Oct 24 20:38:08 UTC 2023


On Tuesday, 24 October 2023 at 15:39:09 UTC, Hipreme wrote:
> On Tuesday, 24 October 2023 at 15:30:33 UTC, Guillaume Piolat 
> wrote:
>> Sorry don't have any issue with T.init.
>> It's not like because it's in D and have not changed for 5 
>> years that it must be bad.
>
> Nope, default initialization is the best thing that could have 
> been created. After people created typescript because not 
> having types was a mistake, the second think we got was default 
> initialization.
>
> The only problem I ever had with D was float being initialized 
> to nan instead of 0. This has been talked a lot of times now.

There are really different ideas that are getting conflated 
because the
implementation of those ideas is so similar. The justification of 
NaN is that
it makes errors more obvious - because failing to initialize 
variables is an
error - which has strange implications for people deliberately 
not initializing
variables in the presence of the feature. In cases other than 
NaN, the default
value at least results in consistent behavior when not 
initializing them was a
logical error.

But,
1. people very naturally rely on this behavior, and deliberately 
not initialize
variables.
2. default initialization prevents tooling like valgrind or 
libsanitizer from
complaining about code relying on uninitialized data. And because 
of #1, these
also can't be adapted to D now.

So I think this error-focused idea is a bad one. Of D's hits and 
misses, it's
really a miss. Maybe that's why Swift and co. are leaving it. 
Nobody's talked
about about their reasons yet.

Fortunately, apart from NaN, it is functionally identical to a 
good idea:
zeroing everything and having the zero value be useful and not an 
error. Zero
Is Initialization (ZII, imitating RAII). The zero list is the 
empty list and
you can push new values onto it. The zero vector is the empty 
vector and you
can do likewise. This isn't automatic, of course - it's very easy 
to write code
that segfaults on zero values. And although Go popularized zero 
values it's
still an error to work with the zero value of a Go hash table.

So I think this idea also hasn't really arrived yet. It's just a 
trick that
some people like to do, and there are some languages that sort of 
do it.




More information about the Digitalmars-d mailing list