Null references redux

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Sep 26 20:20:51 PDT 2009


On Sat, Sep 26, 2009 at 11:06 PM, Jeremie Pelletier <jeremiep at gmail.com> wrote:
>
> I don't want the language to force me to check nullable references before
> using them, that just takes away a lot of optimization cases.

You don't design tight loops that dereference pointers with the
intention that those pointers will ever be null. Those loops always
expect nonnull pointers, and therefore you'd use a nonnull reference.

The number of null references in your program are far less than you'd
think. For those that really could be legitimately null (like an
optional callback or something), you have to check for null at runtime
anyway. Most of your code wouldn't really change. You'd instead just
get more errors at compile time for things that are obviously illegal
or just very potentially dangerous.

> You could just use the casting system to sneak null into a nonnull reference
> and bam, undefined behavior.

No, you couldn't. That would be a pretty shitty nonnull reference type
if the compiler let you put null in it.

> And you could have nullables which are always
> nonnull at some point in time within a scope but your only way out of the
> compiler errors about using a nullable without first testing it for nullity
> is to use excessive casting.

The argument of verbosity that comes up with nonnull references holds
some weight, but it's far more a matter of designing your code not to
do something like that.



More information about the Digitalmars-d mailing list