Non-null objects, the Null Object pattern, and T.init

Namespace rswhite4 at googlemail.com
Sat Jan 18 02:02:21 PST 2014


On Saturday, 18 January 2014 at 06:10:20 UTC, Walter Bright wrote:
> On 1/17/2014 7:38 PM, Walter Bright wrote:
>> But I agree that compile time
>> detect of null reference bugs is better than runtime detection 
>> of them.
>
>
> BTW, the following program:
>
>   class C { int a,b; }
>
>   int test() {
>     C c;
>     return c.b;
>   }
>
> When compiled with -O:
>
>   foo.d(6): Error: null dereference in function _D3foo4testFZi
>
> It isn't much, only working on intra-function analysis and only 
> when the optimizer is used, but it's something. It's been in 
> dmd for a long time.

But:
----
class C { int a, b; }

C create() pure nothrow {
	return null;
}

int test() pure nothrow {
	C c = create();
	return c.b;
}

void main() {
	test();
}
----

Print nothing, even with -O.
Maybe the idea to use C? for nullable references would be worth 
to implement.


More information about the Digitalmars-d mailing list