Can non-nullable references be implemented as a library?

Adam Burton adz21c at gmail.com
Mon Nov 8 11:03:17 PST 2010


Kagamin wrote:

> 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.
Yes, the fact it is a different type is an implementation detail of my 
attempt to also get a basic NN compile-time check.
> 
>> 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?
To get informed by the compiler I am crossing into NN code that I did not 
explicitly authorise. I thought that is a key part of the NN feature 
request. Granted with a template I can't get it inspecting the code around 
it for null checks but by making the cross from null to NN explicit it can 
help people potentially catch code where they've maybe not fully assessed 
the impact of the potential null floating around their code.



More information about the Digitalmars-d mailing list