Search code examples
functionscopedfunction-calls

Mutually Dependent Function Declarations in Functions


Is it possible to have two or more outer function-scoped functions that have mutual call dependencies on each other?

I'm asking because

void main(string args[]) {
    int x = 42;
    void a() {
        // do something with x
        b();
    }
    void b() {
        // do something with x
        a();
    }
}

errors in DMD as

/home/per/Work/justd/t_funs.d(5): Error: undefined identifier b

If not is this an unresolved issue or a feature?


Solution

  • you can declare the delegate b before a and then assign b

    void main(string args[]) {
        int x = 42;
        void delegate() b;
        void a() {
            // do something with x
            b();
        }
        b = delegate void () {
            // do something with x
            a();
        };
    }
    

    it'll require a refactor but not as bad as throwing it all in structs