Why does nobody seem to think that `null` is a serious problem in D?
Sebastiaan Koppe
mail at skoppe.eu
Sat Dec 1 20:41:53 UTC 2018
On Saturday, 1 December 2018 at 19:02:54 UTC, H. S. Teoh wrote:
> But that's precisely the problem. It's not always possible to
> tell whether a variable has been initialized. E.g.:
>
> int func(int x) {
> int *p;
>
> if (solveRiemannHypothesis()) {
> p = &x;
> }
>
> ...
>
> if (solveArtinsConjecture()) {
> *p++;
> }
> return x;
> }
If you are willing to loose some precision you can still analyse
this. Google abstract interpretation.
For instance, after the first if the value of p is (&x || null).
Since the compiler can prove which branch is taken, the analyse
has to assume both are.
Inside the second if, p gets dereferenced, but since p is (&x ||
null) - that is, it might be null - that is a compile time error.
The take away is that you don't need to know what code path will
be taken, you just combine both states.
More information about the Digitalmars-d-learn
mailing list