当未指定返回类型时,DART显示了另一个内部功能的精确返回类型,但对于顶级的动态
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.