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

Per Nordlöw per.nordlow at gmail.com
Thu Nov 22 15:38:18 UTC 2018


On Monday, 19 November 2018 at 21:23:31 UTC, 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.
>
> I'm not the only one who has done this. I can't find it right 
> now, but I've seen at least one person open a bug report 
> because they misunderstood this as a bug in dmd.
>
> I have been told a couple of times that this isn't something 
> that needs to be patched in the language, but I don't 
> understand. It seems like a very easy way to generate a 
> segfault (and not a NullPointerException or whatever).
>
> What's the reasoning for allowing this?

The natural way forward for D is to add static analysis in the 
compiler that tracks use of possibly uninitialized classes (and 
perhaps also pointers). This has been discussed many times on the 
forums. The important thing with such an extra warning is to 
incrementally add it without triggering any false positives. 
Otherwise programmers aren't gonna use it.


More information about the Digitalmars-d-learn mailing list