How do method cascades work exactly in a dart?

As stated in the darts article:

The syntax ".." calls the method (either the installer or the receiver), but discards the result and returns the original receiver instead.

So I suggested that this would work:

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

which gave me an error that I cannot call .addAll on null .

So apparently . precedes .. so .addAll was called as a result of .clear() .

Now I understand that I have two options to write this:

  1. myList..clear()..addAll(otherList);
  2. (myList..clear()).addAll(otherList); (If I wanted to get the result .addAll() .

It is right? If so, why give a decision . precedence? This seems very illogical. To avoid syntax like this: myList(..clear().useResultOfClear()).addAll(otherList); ?

+10
source share
3 answers

You can read an article from Gilad Bracha: The Cascades Method in Dart . At the end you will see many examples.

See also this Lasse Nielsen answer on operator precedence :

This helps to think of ".." as not the operator itself, but rather as a terminal construct (for example, parentheses). It creates a new scope from ".." to the next ".." or the first other scope delimiter (";", ")", "}" or similar).

In principle, a..b().c() coincides with (t){tb().c(); return t;}(a) (t){tb().c(); return t;}(a)

+9
source

As stated in the official text of the Dart Method Cascades in Dart article :

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

In short, method cascades provide syntactic sugar for situations where the recipient of a method call might otherwise repeat.

The following is an example based on / copied from a previously quoted article. Read it for more information.

add() example

In a scenario where you need to add multiple items to a list, the inherited method is to perform multiple assignments:

 myList.add("item1"); myList.add("item2"); // add again and again… myList.add("itemN"); 

Until you can do something like myList.add("item1").add("item1")….add("itemN"); , since add() does not return the method myList object but void , you can use the cascading operator, because it discards the result and returns the original receiver myList :

 myList..add("item1")..add("item2")…..add("itemN"); 
 myList.add("item1").add("item2")….add("itemN"); 

Another example

So instead of writing:

 var address = getAddress(); address.setStreet("Elm", "13a"); address.city = "Carthage"; address.state = "Eurasia" address.zip(66666, extended: 6666); 

Can write

 getAddress() ..setStreet("Elm", "13a") ..city = "Carthage" ..state = "Eurasia" ..zip(66666, extended: 6666); 

Further reading

If you want to know more about the cascade method, I wrote an article adding information that is beyond the scope of this question .

+14
source

I also don't like official docs, but here is my idea, which I think should be official:

.. means keep the previous return value

then for your myList..clear().addAll(otherList);

  1. myList returns itself
  2. (myList)..clear() returns void
  3. (void).addAll() and this is a mistake

or you can think of it as being inside the previous area of ​​the returned object, so that you can constantly access its fields.

0
source

Source: https://habr.com/ru/post/1485446/


All Articles