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

Neia Neutuladh neia at ikeran.org
Mon Nov 19 21:57:11 UTC 2018


On Mon, 19 Nov 2018 21:23:31 +0000, Jordi Gutiérrez Hermoso wrote:
> When I was first playing with D, I managed to create a segfault by doing
> `SomeClass c;` and then trying do something with the object I thought I
> had default-created, by analogy with C++ syntax. Seasoned D programmers
> will recognise that I did nothing of the sort and instead created c is
> null and my program ended up dereferencing a null pointer.

Programmers coming from nearly any language other than C++ would find it 
expected and intuitive that declaring a class instance variable leaves it 
null.

The compiler *could* give you a warning that you're using an uninitialized 
variable in a way that will lead to a segfault, but that sort of flow 
analysis gets hard fast.

If you wanted the default constructor to be called implicitly, that would 
make @nogc functions behave significantly differently (they'd forbid 
declarations without explicit initialization or would go back to default 
null), and it would be a problem for anything that doesn't have a no-args 
constructor (again, this would either be illegal or go back to null).

Easier for everything to be consistent and everything to be initialized to 
null.


More information about the Digitalmars-d-learn mailing list