Method cascading

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

In object-oriented programming, method cascading is syntax which allows multiple methods to be called on the same object. This is particularly applied in fluent interfaces.

For example, in Dart, the cascade:

a..b()
 ..c();

is equivalent to the individual calls:

a.b();
a.c();

Method cascading is much less common than method chaining – it is found only in a handful of object-oriented languages, while chaining is very common. A form of cascading can be implemented using chaining, but this restricts the interface; see comparison with method chaining, below.

Application[edit]

Cascading is syntactic sugar that eliminates the need to list the object repeatedly. This is particularly used in fluent interfaces, which feature many method calls on a single object.

This is particularly useful if the object is the value of a lengthy expression, as it eliminates the need to either list the expression repeatedly or use a temporary variable. For example, instead of either listing an expression repeatedly:

a.b().c();
a.b().d();

or using a temporary variable:

b = a.b();
b.c();
b.d();

cascading allows the expression to be written once and used repeatedly:

a.b()..c()
     ..d();

Comparison with method chaining[edit]

Given a method call a.b(), after executing the call, method cascading evaluates this expression to the left object a (with its new value, if mutated), while method chaining evaluates this expression to the right object.

Chaining

The following chain (in C++):

a.b().c();

is equivalent to the simple form:

b = a.b();
b.c();
Cascading

The following cascade (in Dart):

a..b()
 ..c();

is equivalent to the simple form:

a.b();
a.c();

Cascading can be implemented in terms of chaining by having the methods return the target object (receiver, this, self). However, this requires that the method be implemented this way already – or the original object be wrapped in another object that does this – and that the method not return some other, potentially useful value (or nothing if that would be more appropriate, as in setters). In fluent interfaces this often means that setters return this instead of nothing.

Languages[edit]

Smalltalk[edit]

Method chains and cascades were both introduced in Smalltalk; most subsequent object-oriented languages have implemented chains, but few have implemented cascades. In Smalltalk the semicolon operator can be used to send different messages to the same object:[1]

self listPane parent
  color: Color black;
  height: 17;
  width: 11

Compare with separate statements, terminated with a period, also using a variable for abbreviation:

|parent|
parent := self listPane parent.
parent color: Color black.
parent height: 17.
parent width: 11.

One subtlety is that the value of a method call ("message") in a cascade is still the ordinary value of the message, not the receiver. This is a problem when you do want the value of the receiver, for example when building up a complex value. This can be worked around by using the special yourself method that simply returns the receiver:[2]

Object>>yourself
  ^self

For example, the "add an object to a collection" method (Collection>>add: anObject) returns the object that was added, not the collection. Thus to use this in a cascade in an assignment statement, the cascade must end with yourself, otherwise the value will just be the last element added, not the collection itself:

all := OrderedCollection new
  add: 5;
  add: 7;
  yourself.

Visual Basic[edit]

Visual Basic uses the With statement to enable an arbitrary number of method calls or property accesses on the same object:

With ExpressionThatReturnsAnObject
  .SomeFunction(42)
  .Property = value
End With

With..End With blocks in Visual Basic can be nested:

With ExpressionThatReturnsAnObject
  .SomeFunction(42)
  .Property = value
  With .SubObject
    .SubProperty = otherValue
    .AnotherMethod(42)
  End With
End With

Dart[edit]

Among newer languages, Dart implements cascades, using a double-dot .. "cascaded method invocation operation". Unlike Smalltalk, in Dart the value of a cascaded method invocation is the receiver (base object), not the value of the (uncascaded) method invocation, and thus there is no need for yourself. Dart uses properties, and thus rather than using method syntax for getters and setters (foo.getBar(); foo.setBar(b);), it uses field value/assignment syntax (foo.bar; foo.bar = b;), and cascades work with assignments:

a..string = 'Hello world!'
 ..done = true;

is equivalent to:

a.string = 'Hello world!';
a.done = true;

References[edit]

  1. ^ Beck 1997, "Cascade", pp. 183–185.
  2. ^ Beck 1997, "Yourself", pp. 186–188.
  • Beck, Kent (1997). Smalltalk Best Practice Patterns. Prentice Hall. ISBN 978-0134769042.

External links[edit]

Dart