January 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5503

           Summary: Forward declarations for nested mutually recursive
                    functions
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-01-29 11:31:29 PST ---
(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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------