方法级联如何在飞镖中正常工作? [英] How do method cascades work exactly in dart?

查看:93
本文介绍了方法级联如何在飞镖中正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如飞镖文章所述:

".."语法调用一个方法(或setter或getter),但丢弃结果,并返回原始的接收者.

The ".." syntax invokes a method (or setter or getter) but discards the result, and returns the original receiver instead.

所以我认为这会起作用:

So I assumed that this would work:

myList..clear().addAll(otherList);

这给了我我无法在null上调用.addAll的错误.

which gave me the error that I can't call .addAll on null.

因此,显然...之前,因此.addAll已在.clear()结果上调用.

So apparently . precedes .. so that .addAll has been invoked on the result of .clear().

我现在认为我有两种写法:

I figure now that I have two possibilities to write this:

  1. myList..clear()..addAll(otherList);
  2. (myList..clear()).addAll(otherList);(如果我想获得.addAll()的结果.
  1. myList..clear()..addAll(otherList);
  2. (myList..clear()).addAll(otherList); (If I wanted to get the result of .addAll().

这是正确的吗?如果是,为什么决定赋予.优先权?似乎很违反直觉.是否要避免这样的语法:myList(..clear().useResultOfClear()).addAll(otherList);?

Is this correct? If yes, why the decision to give . precedence? It seems very counterintuitive. Is it to avoid syntax like this: myList(..clear().useResultOfClear()).addAll(otherList);?

推荐答案

您可以阅读Gilad Bracha的文章:

You can read the article from Gilad Bracha : Method Cascades in Dart. At its ends, you will see many examples.

另请参见有关拉瑟尼尔森的答案,运算符优先级:

有助于将".."视为不是真正的运算符,而更像是范围界定结构(如括号).它将创建一个新的范围,从".."到下一个".."或第一个其他范围定界符(;",)",}"或类似名称).

It helps to think of ".." as not really an operator, but more like a scoping construct (like parentheses). It creates a new scope from the ".." to either the next "..", or the first other scope delimiter (";", ")", "}" or similar).

基本上,a..b().c()(t){t.b().c(); return t;}(a)

这篇关于方法级联如何在飞镖中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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