Why dont dlang check NullPointer?

w0rp via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 31 00:48:07 PDT 2015

On Tuesday, 31 March 2015 at 03:58:33 UTC, zhmt wrote:
> Especially working with fibers, ability to catch 
> NullPointerException is more important. If a 
> NullPointerException is caught , only one fiber terminates, 
> otherwise, the whole server crashes.
> If the server is something like 
> webserver(stateless),multi-process is ok. But I am aiming to 
> develope a mmorpg server, it is stateful, so it is not allowed 
> to crash entirely.
> Maybe the solution is to make use of  a script engine (such as 
> lua), but the benefit of choosing dlang is lost.

I argue that instead of catching NullPointerExceptions, you 
should make them never happen. This is where Option types come 
in. Rather than using T*, use an Option!T and force yourself to 
always check for null gracefully. Also use Some!T (a.k.a NotNull) 
and get classes and pointers which are verified via contracts 
that they are not null.

I have already implemented such a thing here.


If you don't like it, you can always implement a similar thing 

Now at this point you might wish for this to gain some benefits 
from being a language feature. I think I will agree, but who 
knows if that will ever happen.

More information about the Digitalmars-d mailing list