Disallow null references in safe code?

Idan Arye GenericNPC at gmail.com
Tue Feb 4 05:11:47 PST 2014


On Tuesday, 4 February 2014 at 02:27:23 UTC, deadalnix wrote:
> 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
> }

Why is `bar(w);` an error? I may be perfectly valid for `bar` to 
accept null as argument.


More information about the Digitalmars-d mailing list