Docs: Section on local variables
Jonathan M Davis
jmdavisProg at gmx.com
Fri Apr 20 18:31:33 PDT 2012
On Saturday, April 21, 2012 02:54:25 Jakob Ovrum wrote:
> 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.
I didn't say that it couldn't. I said that there was no way that it would. It
would break a lot of code, and it goes completely against D's current approach
of default-initialization everything. I'd be shocked if Walter agreed to
making the compiler give an error for variables which aren't either assigned
to or directly initialized before they're used. init solves the problem
already. There's no need to do what C# and Java do on top of that.
> > 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.
Okay. I should have said that Java and C# require that a variable be written
to before being used. Regardless, they take the approach of requiring that a
variable be initialized/assigned a value rather than letting it be garbage
(like C/C++) or initializing it to a default value (like D).
> The control flow graph takes care of correctly determining
> whether a variable has been initialized.
And Walter seems to hate flow analysis like the plague (due to its increased
complexity IIRC). _Very_ little in D requires or does flow analysis.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list