Null references redux

Walter Bright newshound1 at digitalmars.com
Sat Sep 26 18:32:10 PDT 2009


Andrei Alexandrescu wrote:
> Walter Bright wrote:
>> The problem with non-nullable references is what do they default to? 
>> Some "nan" object? When you use a "nan" object, what should it do? 
>> Throw an exception?
> 
> This is the mistake. There would no way to default initialize a non-null 
> object. I'm surprised you are still saying this, because we discussed 
> how NonNull!T could be implemented by disabling its default constructor.

Sure, so the user just provides "0" as the argument to the non-default 
constructor. Or he writes:

     C c = c_empty;

using c_empty as his placeholder for an empty object. Now, what happens 
with:

     c.foo();

? Should c_empty throw an exception? To take this a little farther, 
suppose I wish to create an array of C that I will partially fill with 
valid data, and leave some empty slots. Those empty slots I stuff with 
c_empty, to avoid having nulls. What is c_empty's proper behavior if I 
mistakenly try to access its members?

Forcing the user to provide an initializer does not solve the problem. 
The crucial point is the problem is *not* the seg fault, the seg fault 
is the symptom. The problem is the user has not set the object to a 
value that his program's logic requires.


I am also perfectly happy with NonNull being a type constructor, to be 
used where appropriate. My disagreement is with the notion that null 
references should be eliminated at the language level.



More information about the Digitalmars-d mailing list