Disallow null references in safe code?

deadalnix deadalnix at gmail.com
Mon Feb 3 18:27:22 PST 2014


On Tuesday, 4 February 2014 at 01:09:52 UTC, Meta wrote:
> On Monday, 3 February 2014 at 23:34:59 UTC, deadalnix wrote:
>> On Monday, 3 February 2014 at 22:23:52 UTC, Meta wrote:
>>> If null is an invalid value to assign to a pointer, then 
>>> there's no issue.
>>>
>>> int* foo()
>>> {
>>>   //Error: cannot implicitly convert typeof(null) to type int*
>>>   return "/etc/foo".exists ? new int : null;
>>> }
>>
>> Only cross abstraction boundaries is sufficient.
>
> Can you explain?

void foo() {
      Widget w = null; // OK
      w.foo(); // Error w might be null.

      w = new Widget();
      w.foo(); // OK

      if(condition) {
          w = null;
      }

      w.foo(); // Error

      if(w !is null) {
          w.foo(); // OK
      }

      bar(w); // Error, w might be null=
      return w; // Error, w might be null
}


More information about the Digitalmars-d mailing list