Introducing Nullable Reference Types in C#. Is there hope for D, too?

Timon Gehr timon.gehr at gmx.ch
Fri Nov 17 14:05:48 UTC 2017


On 17.11.2017 12:22, Jonathan M Davis wrote:
> On Friday, November 17, 2017 09:44:01 rumbu via Digitalmars-d wrote:
>> I know your aversion towards C#, but this not about C#, it's
>> about safety. And safety is one of the D taglines.
> 
> Completely aside from whether having the compile-time checks would be good
> or not, I would point out that this isn't actually a memory safety issue.

Memory safety is not the only kind of safety. Also, memory safety is 
usually formalized as (type) preservation which basically says that 
every memory location actually contains a value of the correct type. 
Hence, as soon as you have non-nullable pointers in the type system, 
this _becomes_ a memory safety issue.

> If
> you dereference a null pointer or reference, your program will segfault. No
> memory is corrupted, and no memory that should not be accessed is accessed.
> If dereferencing a null pointer or reference in a program were a memory
> safety issue, then we'd either have to make it illegal to dereference
> references or pointers in @safe code or add additional runtime null checks
> beyond what already happens with segfaults, since aside from having
> non-nullable pointers/references, in the general case, we can't guarantee
> that a pointer or reference isn't null.

There are type systems that do that, which is what is being proposed for 
C#. It's pretty straightforward: If I have a variable of class reference 
type C, it actually contains a reference to a class instance of type C.


More information about the Digitalmars-d mailing list