Memory safe in D

ShowMeTheWay ShowMeTheWay at gmail.com
Wed Apr 17 05:02:20 UTC 2024


On Wednesday, 17 April 2024 at 00:25:07 UTC, Basile B. wrote:
> On Tuesday, 16 April 2024 at 22:15:42 UTC, ShowMeTheWay wrote:
>>
>> btw. This too is a likely bug:
>>
>> int b;
>> writeln(b);
>>
>> The compiler should require you to assign to 'b' before using 
>> it.
>>
>> On the otherhand, this below should *not* get the compilers 
>> attention:
>>
>> int b = int.init;
>> writeln(b);
>
> Both are semantically equivalent. The first version is about 
> knowing how the language works, the second is about being 
> stupid. D policy about default initializers is really to create 
> clear poison value. You still have "void initialization" if you 
> want to introduce UBs.
>
> ```d
> int b = void;
> writeln(b);
> ```
>
> that is more what should get the compiler attention.

btw. I don't see these as semantically equivalent. The compiler 
may, but I don't.

int b;
writeln(b);

vs..

int b = void;
writeln(b);

The first one suggests to me a likely bug.

The second one suggest to me, the programmer has definitely 
assigned a value to b, and whether the writeln code is a bug or 
not depends on what the programmer intended.... I cannot assume 
that a bug was not the intention... perhaps it was.. perhaps it 
wasn't. The compiler would not know the intention of the 
programmer either.

Only in the case of the 'use of an unassigned variable', can the 
compiler reasonbly, and quickly, assume it's a likely bug, and 
that it should alert the programmer.

In the first example, I would want the compiler to alert me.

In the second example, I would want the compiler to get out of 
the way and let me do what I want...



More information about the Digitalmars-d mailing list