[Issue 790] New: arbitrary lookahead for nested functions

BCS nothing at pathlink.com
Wed Jan 3 17:18:31 PST 2007


d-bugmail at puremagic.com wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=790
> 
>            Summary: arbitrary lookahead for nested functions
>            Product: D
>            Version: 0.178
>           Platform: PC
>         OS/Version: Windows
>             Status: NEW
>           Severity: normal
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla at digitalmars.com
>         ReportedBy: Daniel919 at web.de
> 
> 
> void main() {
>         int a() { return b(1); }
>         int b(int y = 0) { if (y == 0) return a(); else return y; }
>         b();
> }
> 
> line 2: Error: undefined identifier b
> 
> If you put the implementation of function a and b outside (into global scope),
> then it's working.
> So the lookahead is not working for nested functions.
> 
> 

Look ahead on nested function is not allowed. Therefor this is according 
to spec.

to quote:

-----------
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.
-------------


More information about the Digitalmars-d-bugs mailing list