www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Nested functions bug

reply Alexei Averchenko <lex.aver mail.ru> writes:
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.
Jan 18 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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.
Jan 18 2007
parent reply Alexei Averchenko <lex.aver mail.ru> writes:
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?
Jan 18 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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.
Jan 18 2007