dart: why local functions can not call each other?

746 views Asked by At
// OK, it works!
void global_1() => global_2();
void global_2() => global_1();

void main() {
    // ERROR Local variable 'local_2' can't be referenced before it is declared.
    void local_1() => local_2(); // <=== ERROR
    void local_2() => local_1();
}

The compiler said "The local variable can't be reference before it is declared"

But why global functions can call each other recursively but local functions can not?

I need to know WHY and some good workarounds for this situation.

1

There are 1 answers

0
Randal Schwartz On

I suspect it depends on scope. global_1 can call global_2 because those are both valid for the duration of the app. But local_2 doesn't exist until it is declared, which is after it is used.