Cascading Messages in Smalltalk

ingve1 pts0 comments

Cascading Messages in Smalltalk | MediumSitemapOpen in appSign up<br>Sign in

Medium Logo

Get app<br>Write

Search

Sign up<br>Sign in

Cascading Messages in Smalltalk

Donald Raab

5 min read·<br>7 hours ago

Listen

Share

Statements end with a period, and messages cascade with a semicolon.

Press enter or click to view image in full size

Cascading message sends to an OrderedCollection

Fluent versus Cascading<br>In a fluent method call chain, messages are sent to the result of the previous message. This is what a fluent method chain looks like in Smalltalk.<br>OrderedCollection new yourself yourself yourself yourself yourself.In this call chain, OrderedCollection is a class. The message new is sent to the OrderedCollection class, and results in a new instance of OrderedCollection being created. Then the message yourself is sent to the new instance of the OrderedCollection. The yourself method would be the equivalent if there were a method named this() in Java that you could call on any object and it would return this. This method (pun intended) doesn’t exist. It would not serve much purpose in Java, without cascading message support.<br>The space between us<br>In Smalltalk, a space character serves the same purpose as the period in Java. The basic syntax is object message, where the space instructs the compiler that the text that follows is a message to send to the receiving object on the left. The Java equivalent of the above Smalltalk code would look as follows.<br>OrderedCollection.new().yourself().yourself().yourself().yourself().yourself().Where are the parentheses in Smalltalk methods?<br>Parentheses are not required for messages in Smalltalk. They are used to resolve order of precedence issues, and for other purposes like creating arrays.<br>A fluent add: method<br>The add: method on OrderedCollection returns the object being added to the collection. If the add: method returned the OrderedCollection itself, then the following would be possible.<br>|oc|<br>oc := (((OrderedCollection new add: 1) add: 2) add: 3) add: 4.The problem as you can see chaining methods like this in Smalltalk that you can’t just say add: 1 add: 2: add: 3 add: 4 because Smalltalk would think that is a single message with four paramters. Parentheses also play a part in making method chaining possible when keyword messages (i.e. messages with parameters) are used.<br>Cascading Messages<br>Cascading messages are a feature I’ve only ever seen in Smalltalk. It’s a neat solution to a common problem. The problem is what to do when you want to send multiple messages to the same object one after another. The classic solution to this problem is to just write separate statements.<br>|collection|<br>collection := OrderedCollection new.<br>collection add: 1.<br>collection add: 2.<br>collection add: 3.<br>collection add: 4.Using cascading messages, you simply list each message to send and then end with a semicolon. Then you send the message yourself, which will have the effect of returning the the original receiver of the messages.<br>|collection|<br>collection := OrderedCollection new<br>add: 1;<br>add: 2;<br>add: 3;<br>add: 4;<br>yourself. An alternate solution with: a single message

Press enter or click to view image in full size

OrderedCollection has a with:with:with:with: method that takes four parameters

As you might have gathered by now, a keyword message uses a colon to indicate there is a parameter that follows, and a multi-parameter keyword message will have a colon for each parameter. The definition of the with:with:with:with: method is as follows in the Collection class.

Press enter or click to view image in full size

The with:with:with:with: method uses a cascaded message send.

The assignment operator, variable definitions, return and self<br>The := is the assignment operator in Smalltalk. Two | characters with named variables in between (e.g.|variable1 variable2|) is how to define local variables in a method.<br>On line four in the method above, the Smalltalk return character is hiding. The ^ is the equivalent of return in Java.<br>The five Smalltalk reserved words<br>There are five reserved words in Smalltalk. The reserved words are self, super, true, false, and nil.<br>Now self is interesting in the method above. In an instance method, self would refer to the instance of the class that the method is bound to. This is the equivalent of this in Java. In a class method, which is what the with:with:with:with: method is above, the self variable refers to the single instance of the Collection class (or subclass in the case of OrderedCollection). There is no equivalent in Java, as this in a static method cannot be used. So self new in this instance is the equivalent of saying OrderedCollection new.<br>That’s All Folks<br>Smalltalk is an odd language when you first read it. It’s syntax is unlike most modern programming languages. By starting with cascading message in this blog I was able to cover a decent amount of the syntax of the Smalltalk language. There is a more comprehensive coverage of the Smalltalk syntax on a post...

method smalltalk message orderedcollection yourself messages

Related Articles