Null references redux

Jeremie Pelletier jeremiep at gmail.com
Sat Sep 26 14:59:45 PDT 2009


Jarrett Billingsley wrote:
> On Sat, Sep 26, 2009 at 5:29 PM, Jeremie Pelletier <jeremiep at gmail.com> wrote:
> 
>> I actually side with Walter here. 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. In most cases a simple backtrace is enough to pinpoint the
>> location of the bug.
> 
> There is NO RUNTIME OVERHEAD in implementing nonnull reference types.
> None. It's handled entirely by the type system. Can we please move
> past this?
> 
>> Null references are useful to implement optional arguments without any
>> overhead by an Optional!T wrapper. If you disallow null references what
>> would "Object foo;" initialize to then?
> 
> It wouldn't. The compiler wouldn't allow it. It would force you to
> initialize it. That is the entire point of nonnull references.

How would you do this then?

void foo(int a) {
	Object foo;
	if(a == 1) foo = new Object1;
	else if(a == 2) foo = Object2;
	else foo = Object3;
	foo.doSomething();
}

The compiler would just die on the first line of the method where foo is 
null.

What about "int a;" should this throw an error too? Or "float f;".

What about standard pointers? I can think of so many algorithms who rely 
on pointers possibly being null.

Maybe this could be a case to add in SafeD but leave out in standard D. 
I wouldn't want a nonnull reference type, I use nullables just too often.



More information about the Digitalmars-d mailing list