How Nested Functions Work, part 1

Nick Sabalausky a at a.a
Wed Sep 2 13:25:25 PDT 2009


"Jarrett Billingsley" <jarrett.billingsley at gmail.com> wrote in message 
news:mailman.32.1251920615.20261.digitalmars-d at puremagic.com...
> On Wed, Sep 2, 2009 at 3:29 PM, Don<nospam at nospam.com> wrote:
>> Michiel Helvensteijn wrote:
>>>
>>> Rainer Deyke wrote:
>>>
>>>> That can lead to subtle problems in the case of functions:
>>>>
>>>> int i = f();
>>>> int f() { return i; }
>>>
>
> It wouldn't even be that difficult. Basically if you treat
> forward-referenced nested functions as a sort of goto, the same rules
> should apply: a call to a nested function may not skip the
> initialization of any variables it depends on. When i's initializer is
> evaluated, it has not been declared yet, so the call to f is illegal.
> It also prevents other invalid use.

Sounds like a good reason to take the C# approach to variable 
initialization: No default values, just disallow reading or taking a 
reference to a var that hasn't *clearly* been initialized.

Or maybe default-init i to int.init, and then set it to f(), but that's 
probably not nearly as good.





More information about the Digitalmars-d mailing list