dart:为什么局部函数不能互相调用? [英] dart: why local functions can not call each other?

查看:88
本文介绍了dart:为什么局部函数不能互相调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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();
}

编译器说局部变量在声明之前不能被引用"

但是为什么全局函数可以递归调用而局部函数却不能?

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.

推荐答案

我怀疑这取决于范围. global_1 可以调用 global_2 ,因为它们在应用程序期间均有效.但是 local_2 在使用之前不存在,直到被声明.

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.

这篇关于dart:为什么局部函数不能互相调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆