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

Timon Gehr timon.gehr at gmx.ch
Fri Nov 17 14:19:25 UTC 2017


On 17.11.2017 03:25, codephantom wrote:
> On Friday, 17 November 2017 at 01:47:01 UTC, Michael V. Franklin wrote:
>>
>> It peeked my interested, because when I first started studying D, the 
>> lack of any warning or error for this trivial case surprised me.
>>
>> // Example A
>> class Test
>> {
>>     int Value;
>> }
>>
>> void main(string[] args)
>> {
>>     Test t;
>>     t.Value++;  // No compiler error, or warning.  Runtime error!
>> }
> 
> 
> Also, if you start with nothing, and add 1 to it, you still end up with 
> nothing, cause you started with nothing. That makes completed sense to 
> me. So why should that be invalid?
> 

Because, for example, 'int' does not have a special null value, and we 
don't want it to have one.

The code starts with nothing, and tries to increment an 'int' Value that 
is associated to nothing. What is this value? There is no null in int. 
And anyway, the code does not say that t is nothing, it says that t is a 
Test. Then it does not say what kind of Test it is. The new features 
allow you to specify that t may be nothing, and they add a type int? 
that carries the cost of a special null value for those who are into 
that kind of thing.


More information about the Digitalmars-d mailing list