Disallow null references in safe code?

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 1 19:39:06 PST 2014


On Saturday, February 01, 2014 10:58:12 Andrei Alexandrescu wrote:
> On 1/31/14, 5:39 PM, Jonathan M Davis wrote:
> > Regardless, we're not adding anything with regards to non-nullable
> > references to the language itself [...]
> 
> I think the sea is changing here. I've collected hard data that reveals
> null pointer dereference is a top problem for at least certain important
> categories of applications. Upon discussing that data with Walter, it
> became clear that no amount of belief to the contrary, personal
> anecdote, or rhetoric can stand against the data.

I'm not sure how I feel about that, particularly since I haven't seen such 
data myself. My natural reaction when people complain about null pointer 
problems is that they're sloppy programmers (which isn't necessarily fair, but 
that's my natural reaction). I pretty much never have problems with null 
pointers and have never understood why so many people complain about them. 
Maybe I'm just more organized in how I deal with null than many folks are. I'm 
not against having non-nullable pointers/references so long as we still have 
the nullable ones, and a lot of people (or at least a number of very vocal 
people) want them, but I'm not particularly enthusiastic about the idea 
either.

> It also became clear that a library solution would improve things but
> cannot compete with a language solution. The latter can do local
> flow-sensitive inference and require notations only for function
> signatures. Consider:
> 
> class Widget { ... }
> 
> void fun()
> {
>      // assume fetchWidget() may return null
>      Widget w = fetchWidget();
>      if (w)
>      {
>          ... here w is automatically inferred as non-null ...
>      }
> }

Yeah, I think that it's always been clear that a library solution would be 
inferior to language one. It's just that we could get a large portion of the 
benefits with a library solution without having to make any language changes. 
It essentially comes down to a question of whether the additional benefits of 
having non-nullable references in the language are worth the additional costs 
that that incurs.

> Bottom line: a language change for non-null references is on the table.
> It will be an important focus of 2014.

Well, I guess that I'll just have to wait and see what gets proposed.

- Jonathan M Davis


More information about the Digitalmars-d mailing list