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

aliak something at something.com
Wed Nov 21 21:05:37 UTC 2018


On Wednesday, 21 November 2018 at 17:46:29 UTC, Alex wrote:
> compiled against 4.6.1 Framework.
>
> However, of course, there is a NullReferenceException, if c 
> happens to be null, when calling baz.
>
> So the difference is not the compiler behavior, but just the 
> runtime behavior...
>
> How could the compiler know the state of Random anyway, before 
> the program run.

The compiler would not be able to prove that something was 
initialized and hence could give an error. Maybe c# doesn't do it 
but swift certainly does:

class C {
     func baz() {}
}

func f() {
     var x: C
     if Int.random(in: 0 ..< 10) < 5 {
         x = C()
     }
     x.baz()
}

error: variable 'x' used before being initialized



More information about the Digitalmars-d-learn mailing list