Bug 7459 – Document the workarounds for mutually-called nested functions.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-07T11:04:00Z
Last change time
2014-04-23T12:18:27Z
Keywords
pull, spec
Assigned to
andrej.mitrovich
Creator
timon.gehr
Comments
Comment #0 by timon.gehr — 2012-02-07T11:04:28Z
d-programming-language.org/function sez:
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;
}
The proposed solution is unnecessarily complicated and non-optimal. Since nested template functions are instantiated with the state of the symbol table of the lexically first instantiation, this is a superior solution:
void test() {
void foo()() { bar(); } // ok
void bar() { foo(); } // ok
}
Comment #1 by hsteoh — 2012-12-13T22:29:06Z
Ideally, the compiler should allow nested functions to call each other. I understand there are some complications with how declarations inside function scope are processed, but is there a way to treat function declarations differently from variable declarations?
Comment #2 by timon.gehr — 2012-12-14T00:20:08Z
(In reply to comment #1)
> Ideally, the compiler should allow nested functions to call each other.
I agree. Furthermore, ideally the state of the symbol table should the one at the declaration of the template.
> I understand there are some complications with how declarations inside function
> scope are processed, but is there a way to treat function declarations
> differently from variable declarations?
There are no complications specific to local declarations, except maybe some kind of conservative definite initalization analysis. I guess it's just not implemented and the spec has been modelled after the implementation. An implementation would have to make sure to handle mixins correctly. (just looking at syntactic properties is not enough to decide whether something can be forward-referenced because of those.)
string foo(){ return "foo"; }
void fun(){
string gen(){ return "void "~foo()~"(){ return `baz`; } ";
mixin(gen());
}
The above code should be a compiler error. (Similar examples can be constructed in unordered scopes.)
Comment #3 by timon.gehr — 2012-12-14T00:21:05Z
(In reply to comment #2)
> ...
>
> The above code should be a compiler error.
/compiler/compile/s
Comment #4 by andrej.mitrovich — 2013-02-06T20:38:11Z
(In reply to timon.gehr from comment #0)
> d-programming-language.org/function sez:
>
> Unlike module level declarations, declarations within function scope are
> processed in order. This means that two nested functions cannot mutually
> call each other:
>
[snip]
>
> The proposed solution is unnecessarily complicated and non-optimal. Since
> nested template functions are instantiated with the state of the symbol
> table of the lexically first instantiation, this is a superior solution:
>
> void test() {
> void foo()() { bar(); } // ok
> void bar() { foo(); } // ok
> }
In issue 12578, I'm proposing a language behavior to accept the code.