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

Alex sascha.orlov at gmail.com
Wed Nov 21 11:53:14 UTC 2018


On Wednesday, 21 November 2018 at 10:47:35 UTC, NoMoreBugs wrote:
> On Monday, 19 November 2018 at 21:39:22 UTC, Adam D. Ruppe 
> wrote:
>> On Monday, 19 November 2018 at 21:23:31 UTC, Jordi Gutiérrez 
>> Hermoso wrote:
>>> What's the reasoning for allowing this?
>>
>> The mistake is immediately obvious when you run the program, 
>> so I just don't see it as a big deal. You lose a matter of 
>> seconds, realize the mistake, and fix it.
>>
>> What is your proposal for handling it? The ones usually put 
>> around are kinda a pain to use.
>
> How hard would it be, really, for the compiler to determine 
> that c was never assigned to, and produce a compile time error:
>
> "c is never assigned to, and will always have its default value 
> null"
>
> That doesn't sound that hard to me.

Am I misled, or isn't this impossible by design?

´´´
import std.stdio;
import std.random;

class C
{
	size_t dummy;
	final void baz()
	{
		if(this is null)
		{
			writeln(42);
		}
		else
		{
			writeln(dummy);
		}
	}
}
void main()
{
	C c;
	c.foo;
}

void foo(ref C c)
{
	if(uniform01 < 0.5)
	{
		c = new C();
		c.dummy = unpredictableSeed;
	}
	c.baz;
}
´´´


More information about the Digitalmars-d-learn mailing list