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

Reply via email to