Questions tagged [method-cascades]

Method cascades are a syntactic nicety to avoid having to repeat the receiver of a series of methods.

Method cascades are a syntactic nicety to avoid having to repeat the receiver of a series of methods. They notably exist in and .

In Smalltalk, this:

| window |
 window := Window new.
 window label: 'Hello'.
 window open

can be cascaded like this:

Window new
  label: 'Hello';
  open

In Dart, this:

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

Can be cascaded like this:

getAddress()
  ..setStreet(“Elm”, “13a”)
  ..city = “Carthage”
  ..state = “Eurasia”
  ..zip(66666, extended: 6666);
4 questions
36
votes
5 answers

How do method cascades work exactly in dart?

As stated in the dart article: 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); which gave me the…
enyo
  • 16,269
  • 9
  • 56
  • 73
7
votes
2 answers

Simulating method cascades in C#

The Dart programming language has support for method cascades. Method cascades would allow the following Silverlight/WPF C# code: var listBox = new ListBox(); listBox.Width = 200; listBox.MouseEnter += (s, e) =>…
dharmatech
  • 8,979
  • 8
  • 42
  • 88
3
votes
1 answer

Any way to get the return value of last method in cascade?

I'm doing something like this: new A() ..methodA() ..methodB() .toString(); Should this return the result of toString()? Currently it's returning the new A object.
Daniel Robinson
  • 13,806
  • 18
  • 64
  • 112
1
vote
1 answer

How do I reference "this" while method cascading in Dart?

I'd like to reference "this" (a method owner) while method cascading in Dart. // NG code, but what I want. paragraph.append( getButton() ..text = "Button A" ..onClick.listen((e) { print (this.text + " has been has clicked"); // <=…