Nested functions bug

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Jan 18 05:37:50 PST 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.


More information about the Digitalmars-d-bugs mailing list