How does D handle null pointers?

Jonathan M Davis jmdavisprog at gmail.com
Fri Aug 20 17:30:05 PDT 2010


On Friday, August 20, 2010 17:13:44 bearophile wrote:
> Jonathan M Davis:
> > Considering that other major languages such as C# and Java do these
> > checks with every dereference (though they may optimize it out in some
> > cases), I don't think that it's at all unreasonable to have them in D.
> > They'd be a huge boon to debugging. Now, since D is as concerned about
> > efficiency as it is (being a systems language), it makes sense that
> > those checks would be removed in release mode, but I still think that it
> > would be a very good idea to have them in debug mode.
> 
> C# and Java are badly designed, D may do better. In my opinion a better
> solution to this problem is to: 1) Introduce non-nullable
> pointers/references, to denote them I have proposed a trailing @. 2)
> Require explicit tests every time a nullable pointers/references is about
> to be dereferenced (and then after this test in the else branch the
> reference type "becomes" a non-nullable one. This is an application of the
> idea of "TypeState", used by the Mozilla Rust language. The type doesn't
> actually change, it's just its state that changes...).
> 
> This way you don't need (most) implicit null checks.
> 
> Bye,
> bearophile

It sounds like it's essentially a way to indicate to the compiler how to 
optimize out non-essential null checks. Since, optimizations are only done in 
release mode (unless your masochistic anyway), that means that such a feature 
would be oriented towards release mode, though maybe it could be done in debug 
mode because the checks aren't explicitly in the code.

Still, it sounds like you're essentially saying that we should have always check 
unless we can determine that we don't have to. That's fine for debug mode but 
debatable for release mode. I'd probably be for it in most cases if it really 
were removing most checks, but we'd still need to be able to remove the checks 
completely.

Regardless, I'm not really all that familiar with what you mean by non-nullable 
pointers/references here. My first thought would have been that it would be kind 
of like a reference in C++ except that you could reassign to it later (as long 
as you assigned a valid address to it rather than null). But what you describe 
sounds more like a method for optimizing out null checks after having put them 
everywhere.

- Jonathan M Davis


More information about the Digitalmars-d mailing list