Disallow null references in safe code?

Meta jared771 at gmail.com
Sat Feb 1 21:10:00 PST 2014


On Saturday, 1 February 2014 at 22:05:21 UTC, Adam D. Ruppe wrote:
> 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.

This is a common staple of languages with more advanced type 
systems that support flow-sensitive typing. Ceylon is an upcoming 
language that I'm quite excited about that features this.

"Typesafe null and flow-sensitive typing" section
http://www.ceylon-lang.org/documentation/1.0/introduction/



More information about the Digitalmars-d mailing list