Disallow null references in safe code?

Adam D. Ruppe destructionator at gmail.com
Sat Feb 1 14:05:20 PST 2014


On Saturday, 1 February 2014 at 18:58:11 UTC, Andrei Alexandrescu 
wrote:
>     Widget w = fetchWidget();
>     if (w)
>     {
>         ... here w is automatically inferred as non-null ...
>     }

A library solution to this exists already:

Widget wn = fetchWidget();
if(auto w = wn.checkNull) {
    // w implicitly converts to NotNull!Widget
}

I've had some difficulty in const correctness with my 
implementation... but const correct is an entirely separate issue 
anyway.

It isn't quite the same as if(w) but meh, does that matter? The 
point of the static check is to make you think about it, and 
that's achieved here.


If we do want to get the if(w) to work, I'd really prefer to do 
that as a library solution too, since then we might be able to 
use it elsewhere as well. Maybe some kind of template that lets 
you do a scoped transformation of the type. idk really.


More information about the Digitalmars-d mailing list