Thread overview
Nested functions bug
Jan 18, 2007
Alexei Averchenko
Jan 18, 2007
Frits van Bommel
Jan 18, 2007
Alexei Averchenko
Jan 18, 2007
Frits van Bommel
January 18, 2007
I discovered an issue with nested functions: they must be declared before used. Is this a bug or a lenguage limitation. Whatever it is, please resolve this issue, because it screwes the very idea of nested functions, making it hard to read and maintain the code. Thanks in advance.
January 18, 2007
Alexei Averchenko wrote:
> I discovered an issue with nested functions: they must be declared
> before used. Is this a bug or a lenguage limitation. Whatever it is,
> please resolve this issue, because it screwes the very idea of nested
> functions, making it hard to read and maintain the code. Thanks in
> advance.

http://www.digitalmars.com/d/function.html#nested (at the end of the section):

*****
Unlike module level declarations, declarations within function scope are processed in order. This means that two nested functions cannot mutually call each other:

void test()
{
    void foo() { bar(); }	// error, bar not defined
    void bar() { foo(); }	// ok
}

The solution is to use a delegate:

void test()
{
    void delegate() fp;
    void foo() { fp(); }
    void bar() { foo(); }
    fp = &bar;
}

Future directions: This restriction may be removed.
*****

So it's a limitation. Someday it may be removed, but until then there's a workaround available.
January 18, 2007
Frits van Bommel Wrote:

> Unlike module level declarations, declarations within function scope are processed in order. This means that two nested functions cannot mutually call each other:

I understand. But the thing is I can't call nested function even from the function in which it was declared before the declaration. Is this the limitation too?
January 18, 2007
Alexei Averchenko wrote:
> Frits van Bommel Wrote:
> 
>> Unlike module level declarations, declarations within function scope are processed in order. This means that two nested functions cannot mutually call each other:
> 
> I understand. But the thing is I can't call nested function even from the function in which it was declared before the declaration. Is this the limitation too?

I think that's the *entire* limitation. Not being able to call it from a nested function defined before it is just one manifestation of it.