带有/不带有关键字`new`的Dart工厂构造函数,有什么区别?

编程入门 行业动态 更新时间:2024-10-28 04:19:39
本文介绍了带有/不带有关键字`new`的Dart工厂构造函数,有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从 flutter_redux 示例代码.很难理解为什么 factory SearchState.initial()返回 new 关键字,而 factory SearchState.loading()和不需要工厂SearchState.error().

I came across the following code example from flutter_redux example code. Had a hard time to understand why factory SearchState.initial() returns with a new keyword while factory SearchState.loading() and factory SearchState.error() don't.

class SearchState { final SearchResult result; final bool hasError; final bool isLoading; SearchState({ this.result, this.hasError = false, this.isLoading = false, }); factory SearchState.initial() => new SearchState(result: SearchResult.noTerm()); factory SearchState.loading() => SearchState(isLoading: true); factory SearchState.error() => SearchState(hasError: true); }

只是发现Dart语言之旅对这种情况不是很有帮助,而且Dart语言规范太晦涩.

Just found the Dart language tour not very helpful to this case, and the Dart language specification is too obscure.

推荐答案

有效的飞镖指南:

Dart 2使new关键字为可选.即使在Dart 1中,其含义也是永远不清楚,因为工厂构造函数意味着可能会进行新的调用仍然没有真正返回新对象.

Dart 2 makes the new keyword optional. Even in Dart 1, its meaning was never clear because factory constructors mean a new invocation may still not actually return a new object.

该语言仍然允许新的语言,以减少迁移很痛苦,但是请不要使用它,并将其从您的代码中删除.

The language still permits new in order to make migration less painful, but consider it deprecated and remove it from your code.

更多推荐

带有/不带有关键字`new`的Dart工厂构造函数,有什么区别?

本文发布于:2023-07-28 14:03:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不带   有什么区别   函数   关键字   工厂

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!