Null references redux

Denis Koroskin 2korden at gmail.com
Sat Sep 26 14:46:10 PDT 2009


On Sun, 27 Sep 2009 01:29:55 +0400, Jeremie Pelletier <jeremiep at gmail.com>  
wrote:
> [...] I much prefer my programs to crash on using a null reference and  
> fix the issue than add runtime overhead that does the same thing.

What runtime overhead are you talking about here? Use of non-null pointers  
actually make your program run faster, because you don't have to check  
them against null all the time. Non-null references is a contract, which  
is enforced by a compiler at compile-time, not runtime. It also makes your  
program more consistent and less verbose.

>
> Null references are useful to implement optional arguments without any  
> overhead by an Optional!T wrapper.
>

Once again, what overhead are you talking about? Optional!(T) (or  
Nullable!(T)) doesn't have to have any additional bits to store the NULL  
state for reference types.

> If you disallow null references what would "Object foo;" initialize to  
> then?
>

Nothing. It's a compile-time error. But the following is not:

Object foo = initializer();
Nullable!(Object) foo2; // default-initialized to a null, same as currently
Object? foo3; // a desirable syntax sugar for Nullable!(Object)



More information about the Digitalmars-d mailing list