Introducing Nullable Reference Types in C#. Is there hope for D, too?

rumbu rumbu at rumbu.ro
Mon Nov 20 08:49:41 UTC 2017


On Monday, 20 November 2017 at 06:24:31 UTC, Tobias Müller wrote:
> Timon Gehr <timon.gehr at gmx.ch> wrote:
>>> I wish there was a null for int types.
>> 
>> AFAIU, C# will now have 'int?'.
>
> C# had 'int?' (nullable value types) for ages.
> The new thing is explicitly nullable classes (reference types). 
> I'm really
> looking forward to use those.

int? is just syntactic sugar for Nullable<int>. It has been 
around since 2005. Nullable<T> is just a struct with an 
implementation similar to Nullable!T from D's std.typecons

This topic is about a new C# feature called "nullable reference 
types":

1. if you declare SomeClass x, x is assumed to *not hold null 
values*, that means that when you try "x = null" or x =" 
somepossiblenullvalue", this will result in a compiler warning: 
"Warning, x is supposed to hold a value" The warning can be 
avoided by using "x = null!" or "x = somepossiblenullvalue!"

2.if you declare SomeClass? x, x is allowed to *hold null 
values*, meaning that if you try "x.someFunction()", this will 
result in a compiler warning: "Warning, x can be null". The 
warning can be avoided in two ways:

2a. test for null: "if (x != null) { x.someFunction(); }"
2b. show the compiler that you know better:: x!.someFunction()


In fact, this is the introduction of a new operator "!", probably 
named "I know better" operator.



More information about the Digitalmars-d mailing list