Default initialization no longer a thing

claptrap clap at trap.com
Mon Oct 23 20:34:31 UTC 2023


On Monday, 23 October 2023 at 18:54:45 UTC, IGotD- wrote:
>
> Now even more modern languages are going away from default 
> initialization as some claim that default initialization was a 
> source of bugs. The implementation might differ, some must give 
> a variable a value at declaration others detect use before 
> assign.

The bug is using a variable before it has been assigned the 
**correct** value. Default initialisation doesn't solve that 
problem, only makes the side effects less onerous. Less likely to 
give weird results.

If you have a variable that needs to be initialised via a large 
switch, you declare it up front, and it is set in the switch. It 
would be more useful to know if you forgot to set it in one of 
the case statements that it would be to have it default 
initialised to zero or be told it needs to be initialised at the 
declaration.

But tracking the use of uninitialized variables like that is 
probably pretty difficult / expensive for the compiler.


More information about the Digitalmars-d mailing list