How does D handle null pointers?

bearophile bearophileHUGS at lycos.com
Fri Aug 20 16:16:33 PDT 2010


Adam Ruppe:

> The way I solved it was to sprinkle
> assert( var !is null );
> before I start using or storing the thing, and in the contracts to the
> functions.

Yes, contracts reduce this problem a lot. Recently here I have shown a paper about this topic.

-----------------

Jonathan M Davis:

>There are quite a few people who want Walter to put in null checks in at least debug mode, but he's against it and no one has been able to convince him.<

I think that Walter is right here, adding tons of null checks in debug mode helps a bit (stack traces are good), but it's just a rough patch on the problem, but it's not a good solution of it.

In my opinion a better solution to this problem is to:
1) Introduce nonnullable pointers/references, to denote them I have proposed a leading @.
2) Require explicit tests every time a nullable pointers/references is about to be deferenced (and then after this test in the else branch the reference type "becomes" a nonnullable 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...).

Bye,
bearophile


More information about the Digitalmars-d mailing list