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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Nov 22 01:48:55 UTC 2017


On Wednesday, November 22, 2017 01:25:48 codephantom via Digitalmars-d 
wrote:
> On Wednesday, 22 November 2017 at 00:49:02 UTC, Jonathan M Davis
>
> wrote:
> > The question isn't whether we should use the type system to
> > prevent bugs. The question is which set of problems really make
> > sense to prevent with the type system.
> >
> > - Jonathan M Davis
>
> Those that can be proven.

Sure. If it can't be proven that something is a bug, then the compiler
shouldn't be giving an error in that case (and IMHO, it shouldn't be warning
about it either, since any good programmer doesn't leave warnings in their
project, effectively making warnings errors).

In the case of null, you _can_ prove it if you have non-nullable types. If
it's not legal for a pointer or reference to be null, then the compiler can
guarantee that it's not null. But then you either have the extra
complication of having both nullable and non-nullable pointers/references in
the language, or you force all pointers/references to use something like
std.typecons.Nullable to treat them as nullable or use a construct in the
language which does the same, and that arguably doesn't make a lot of sense
given that underneath the hood, all pointers or references are going to be
nullable, even if you're not allowed to make them null by the type system.
But it would reduce the amount of code where you would have to worry about
potentially having null values.

However, if you don't have non-nullable pointers/references, then you really
can't prove that a pointer or reference is non-null in the general case. You
can prove it under certain circumstances, but ultimately you're going to end
up with an algorithm that only works part of the time. So, best case, it
gives you an error when it definitively knows that you're trying to
dereference null, but that would likely generally be in the cases where you
would very quickly find it yourself as soon as you ran your code. So, while
the compiler check might be useful, I doubt that it would ultimately help
much with preventing bugs in practice.

- Jonathan M Davis



More information about the Digitalmars-d mailing list