Disallow null references in safe code?

Meta jared771 at gmail.com
Mon Feb 3 14:23:51 PST 2014


On Monday, 3 February 2014 at 22:18:35 UTC, Jonathan M Davis 
wrote:
> For it to know, it would have to examine the body of foo (which 
> it doesn't
> necessarily have the code for under C's compilation model - 
> which D uses), and
> even if it did that wouldn't be enough e.g.
>
> int* foo()
> {
>     return "/etc/foo".exists ? new int : null;
> }
>
> The compiler could flag that as _possibly_ returning null and 
> therefore the
> previous code _possibly_ dereferencing null, but it can't know 
> for sure.

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;
}


More information about the Digitalmars-d mailing list