Memory safe in D

Alex akornilov.82 at mail.ru
Mon Mar 11 11:01:59 UTC 2024


On Monday, 11 March 2024 at 10:45:27 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
> On 11/03/2024 11:39 PM, Alex wrote:
>> On Monday, 11 March 2024 at 10:31:05 UTC, Richard (Rikki) 
>> Andrew Cattermole wrote:
>>> On 11/03/2024 11:20 PM, Alex wrote:
>>>> Oh... looks like null is also used for refs in D. It's sad :)
>>>> I thought it used only for pointers in unsafe mode.
>>>> I think, the null safety feature is very important in modern 
>>>> world (maybe "must have" :) ). Very nice to have such 
>>>> feature in D like in Kotlin for example.
>>>> So, as I understand, D team have the task in TODO list about 
>>>> implementation something like "null safety"?
>>>
>>> I'm not sure I'd call myself part of the core D team 
>>> (although I have another proposal that is currently going 
>>> through the DIP process that would certainly qualify me for 
>>> the title!).
>>>
>>> However in saying that, memory safety is on the foundation's 
>>> radar as needing solving.
>>>
>>> I'm just the weirdo that is having a go at trying to solve 
>>> temporal memory safety (an unsolved problem!).
>> 
>> Thank you for the information!
>> Maybe you know: are there some guys from D foundation here?
>
> Yes, they are around including Walter, I'm sure he'll see it 
> within the day.
>
>> Also, I figured out that I can't handle uninitialized access 
>> via try/catch:
>> 
>> ```d
>> A a;
>> try {
>>      a.run();
>> } catch(Throwable) {
>>      writeln("Error");
>> }
>> ```
>> 
>> So the catch branch not work here.
>
> The a variable was initialized, via default initialization.
>
> It is in a known state, null.
>
> What you are wanting is a way to have the compiler complain 
> when a nonnull type state is expected, but it is initialized.
>
> D does not support that currently.

Yes, I got it about compiler, static analyzer can't detect such 
potential issue for now.
The instance of class `A` is initialized by default initializer - 
correct?.
But what about variable `a`?
Is it initialized by null or contains reference to the instance 
initialized by default initializer?
What happend when I tried to call method `run()` of `a` in 
runtime?
I see that application was abnormal termination because 
`writeln("Hello, world!");` was not called.
But I don't see any information in console about it (backtrace or 
something else).
Is it uncatched excpetion? But I have tried to catch it - not 
work.


More information about the Digitalmars-d mailing list