Null references redux

Jason House jason.james.house at gmail.com
Sat Sep 26 21:08:19 PDT 2009


Walter Bright Wrote:

> Denis Koroskin wrote:
> > One more:
> > 
> > T foo(bool someCondition)
> > {
> >     T? t;
> >     if (someCondition) t = someInitializer();
> >     // ...
> > 
> >     if (t.isNull) { // not initialized yet
> >         // ...
> >     }
> > 
> >     return enforce(t); // throws if t is not initialized yet, because 
> > foo *must* return a valid value by a contract
> > }
> 
> It seems to me you've got null references there anyway?
> 
> What would you do about:
> 
>     T[] a;
>     a[i] = foo();
> 
> where you want to have unused slots be null (or empty, or nothing)?

Your example segfaults. A is null.

If T is const or immutable, special care is also required.



More information about the Digitalmars-d mailing list