Why does nobody seem to think that `null` is a serious problem in D?

Kagamin spam at here.lot
Thu Nov 22 09:07:34 UTC 2018


On Wednesday, 21 November 2018 at 17:00:29 UTC, Alex wrote:
> This was not my point. I wonder, whether the case, where the 
> compiler can't figure out the initialization state of an object 
> is so hard to construct.
>
> ´´´
> import std.experimental.all;
>
> class C
> {
> 	size_t dummy;
> 	final void baz()
> 	{
> 		if(this is null)
> 		{
> 			writeln(42);
> 		}
> 		else
> 		{
> 			writeln(dummy);
> 		}
> 	}
> }
> void main()
> {
> 	C c;
> 	if(uniform01 < 0.5)
> 	{
> 		c = new C();
> 		c.dummy = unpredictableSeed;
> 	}
>         else
>         {
>                 c = null;
>         }
> 	c.baz;
> 	writeln(c is null);
> }
> ´´´
>
> C# wouldn't reject the case above, would it?

As `c` is initialized in both branches, compiler knows it's 
always in initialized state after the if statement.


More information about the Digitalmars-d-learn mailing list