Forward declaration inside Function block, no error?

Neia Neutuladh neia at ikeran.org
Sun Jan 6 22:33:04 UTC 2019


On Sun, 06 Jan 2019 20:19:59 +0000, Rubn wrote:
> You can declare functions inside of functions in D. You weren't forward
> declare grow() in the module namespace, so much as you were forward
> declaring a new function grow.

Unfortunately, you can't do forward declarations for nested functions. If 
you could, that would be handy for mutual recursion:

    void main()
    {
        int a(int i);
        int b(int i)
        {
            if (i > 0) return a(i - 1);
            return abs(i);
        }
        int a(int i)
        {
            if (i % 2 == 0) return b(i - 2);
            return b(i - 1);
        }
        writeln(a(12));
    }

Unfortunately, Error: declaration a is already defined

And omitting the forward declaration gets you: Error: undefined identifier 
a


More information about the Digitalmars-d-learn mailing list