Can non-nullable references be implemented as a library?

Kagamin spam at here.lot
Sun Nov 7 23:52:31 PST 2010


Adam Burton Wrote:

> The above seems correct to me. You are assigning a nullable to a non-
> nullable so you force the user to assess that is correct and provide an 
> override. Based on that I've had a crack at this myself.

This becomes not just an annotation, but another type. When you pass argument by ref, you don't have to convert it to ref, you just pass it.

> NN!Super without a null check, you only check for nulls when crossing from 
> the nullable world to non-nullable using toNN. It also means code like below 
> is not possible without explicitly saying I want to cross from nullable to 
> non-nullable (using toNN) which offers some sort of compile time check (not 
> as good as a compiler checking for "if is null" but it is something).
> 
> A a = null;
> NN!A b = a;	// will not compile
> NN!A b = toNN(a);	// inserts the null runtime check
> NN!A c = b; // No null check as there is no need.

This way or another, you need a null check. Why extra syntax?


More information about the Digitalmars-d mailing list