当未指定返回类型时,DART显示了另一个内部功能的精确返回类型,但对于顶级的动态

发布于 2025-02-08 12:59:32 字数 268 浏览 1 评论 0原文

vscode显示字符串用于 bar 的返回类型,动态用于 foo 的返回类型。

void main() {
  bar() => 'bar'; // String bar()
}

foo() => 'foo'; // dynamic bar()

VSCode shows String for return type of bar, and dynamic for return type of foo.

void main() {
  bar() => 'bar'; // String bar()
}

foo() => 'foo'; // dynamic bar()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2025-02-15 12:59:32

DART的类型推理算法在顶级声明和本地声明方面有所不同。
这是因为顶级声明都可以互相指代,而本地声明只能指较早的本地声明(或最高级别的声明,但首先推断出这些声明,因此它们在这一点上是安全的)。

因此,顶级推断不像本地类型的推理那样聪明,但在合理的时间内也是可决定的。

在顶级和班级声明上写类型。更安全。

Dart's type inference algorithm differs for top-level declarations and local declarations.
That's because top-level declarations can all refer to each other, while local declarations can only refer to earlier local declarations (or to top-level declarations, but those are inferred first, so they're safe at this point).

Because of that, the top-level inference is not as clever as the local type inference, but it's also decidable in reasonable time.

Write types on top-level and class level declarations. It's safer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文