Comment #0 by bearophile_hugs — 2011-01-29T11:31:29Z
(I am not sure if this is a good idea or if it's worth the work to implement it. But here it is.)
Currently you are allowed to write forward declarations inside nested functions, despite they are not so useful:
Andrej Mitrovic:
> Well, you might be linking to an external function /and/ don't want
> the function to be visible at module scope:
>
> void main() {
> extern(C) double func(); // linked from some C library..
> double result = func();
> }
Currently to write mutually recursive nested functions you need to use delegates this way:
import std.stdio: writeln;
void main() {
// Hofstadter Female and Male sequences
static int delegate(int) M;
static int delegate(int) F;
F = (int n) {
return n ? n - M(F(n - 1)) : 1;
};
M = (int n) {
return n ? n - F(M(n - 1)) : 0;
};
foreach (i; 0 .. 100)
writeln(F(i));
}
So a possible enhancement request is to allow this usage of nested forward declarations:
import std.stdio: writeln;
void main() {
// Hofstadter Female and Male sequences
int M(int);
static int F(int n) {
return n ? n - M(F(n - 1)) : 1;
}
static int M(int n) { // line 11
return n ? n - F(M(n - 1)) : 0;
}
foreach (i; 0 .. 100)
writeln(F(i));
}
In DMD 2.051 this last code generates:
test.d(11): Error: declaration M is already defined
Comment #1 by ketmar — 2016-06-07T20:09:41Z
2016. problem is not solved yet. there is no good reason for nested functions to ignore "declaration order doesn't matter" rule. yes, local variables ignoring it. but nested functions are not local variables, and having no way to write mutually recursive nested functions is a big limitation.
Comment #2 by robert.schadek — 2024-12-13T17:54:46Z