Navigation:  Tutorials > Beginners Guide to Smalltalk > Objects and Messages >

Cascading Messages

Previous pageReturn to chapter overviewNext page

Let's try creating a new window and manipulating it. Evaluate the following lines individually to see the incremental effect:

window := ShellView new show.


At this point you will have to switch back to the workspace since your new window will be brought to the front. Let's continue:

window position: 80@80.

window extent: 320@90.

window backcolor: Color red.

window caption: 'My Red Test Window'.


We are sending a sequence of messages to the same receiver. It's quite convenient that Smalltalk offers a shorthand way of doing this, by cascading the messages to a common receiver. The following code performs a similar sequence but must be evaluated in one go since all the lines are effectively part of a single expression.

window := ShellView new show.


       position: 80@80;

       extent: 320@90;

       backcolor: Color blue;

       caption: 'My Blue Test Window'.


Here, each of the cascading messages is separated by a semicolon. Each is sent in turn to the object held in the window variable. The lines are intended by one tab position merely as a matter of Smalltalk style to make the code more readable.

Remember to close your red and blue windows now you've finished with them.

So what is the result of a series of cascading messages? Well, it's the answer from the last message in sequence. Try this:

rect := Rectangle origin: 0@0 extent: 10@10.

rect area


We've created a Rectangle object with an area of 100 square units. Now let's modify the rectangle to be eight units square and re-compute the area. Once again you'll have to evaluate this as a single expression:


       top: 1;

       left: 1;

       bottom: 9;

       right: 9;



The result displayed is that answered by the last message in sequence, area.