On Friday, 20 April 2012 at 18:40:46 UTC, Jonathan M Davis wrote:
But there's no way that the compiler is going to enforce
that, and if it did, it would require that you initialize the
variable in the
above example even though the compiler already does.
Why can't the compiler enforce it? C# and Java compilers can do
it just fine.
It seems to me that there are two approaches for the language
to take:
1. Force all variables to be explicitly initialized, even if
they don't really
need to be (but the compiler can't always tell for sure). Java
and C# take
this approach.
Java and C# do not do that. They just enforce that local
variables have to be written to at least once before being read.
The control flow graph takes care of correctly determining
whether a variable has been initialized.