-
Notifications
You must be signed in to change notification settings - Fork 9
Description
In Elixir we are use to apply multiple functions on a value with the pipe operator |>
"Hello there"
|> String.upcase()
|> String.split()
The pipe operator takes the returned value of the function and apply the next function. This works nicely because of Elixir values are immutable and functions return a new value with the updated change from the functions.
Dart is object oriented. This means that the following doesn't work:
var l = [1,2,3];
l.add(4).add(5);
Here l is an list and the add
function change the state of the list directly, it doesn't create a new list value. Moreover the add
function as the following declaration:
void add(E value)
So add
returns a void
value and not the list itself.
One solution to add multiple values in a list could be:
void main(){
var a = [1,2,3];
a.add(4);
a.add(5);
print(a);
}
However the cascade operator ..
is here to make the code a be nicer.
The operator apply a method on the receiver/object, discard the returned value of that method and instead returns the receiver/object itself.
void main(){
var a = [1,2,3]
..add(4)
..add(5);
print(a);
}
ref: