[ Content | View menu ]

Smalltalk cascades

Written on 11 Feb 2006

Jay Fields writes about using chaining for object initialisation.

One of the inspirations for the fluent interface style in jMock was a Smalltalk construct called cascade, which sends multiple message to the same object.

aThing one: 'one';
  two: 'two';
  other.

I just got fed up having to redeclare the same object

thing.setOne("one");
thing.setTwo("two);

Filed in: Smalltalk roots.

7 Comments

Comments closed

  1. Comment by Robert Chatley:

    I like this. I’ve been trying to use a similar style in writing functional tests recently, to make them more readable (more like English sentences).

    In the Martin Fowler article you link to, he says “I saw Steve Freeman and Nat Price give an excellent talk at JAOO2005 on the evolution of the JMock API, they promised to write up their experiences but haven’t. Someone please kidnap their compilers until they do.” Ahem.

    11 Feb 2006 @ 12:57
  2. Comment by Bill Caputo:

    I’ll second that: Ahem

    12 Feb 2006 @ 02:43
  3. Comment by Steve Freeman:

    Thanks guys.

    I’ve got a little time coming up so, unless I get swamped with admin, maybe I can finally get something done.

    12 Feb 2006 @ 16:31
  4. Comment by Steve Freeman:

    OK. We’ve written up a paper and submitted it to OOPLSA for this year. We’ll post as much as we can as soon as they let us know.

    26 Mar 2006 @ 02:29
  5. Comment by Tommy Carlier:

    The StringBuilder-class in .NET has some methods that return the instance itself, mimicking cascading:

    StringBuilder sb = new StringBuilder();
    sb
    .Append(a)
    .Append(b)
    .Append©;

    Off course, this is pretty artificial, and only works on classes that are designed that way and when your methods don’t return anything else.

    10 Apr 2006 @ 17:50
  6. Comment by Steve Freeman:

    Yes. The Java equivalent does the same, maybe because someone inherited an idea from C++ streams? It just saves so much noise in the code.

    10 Apr 2006 @ 21:36
  7. Comment by Steve Freeman:

    Yes. The Java equivalent does the same, maybe because someone inherited an idea from C++ streams? It just saves so much noise in the code.

    10 Apr 2006 @ 21:36