Cincom

Primer : Smalltalk Syntax (Part 1)


In Smalltalk, there are only 3 types of messages. Classifying these types is important because there are rules as to how complex statements eventually get broken down into the basic Object - Method building block of the language.

This primer explains the 3 types of Smalltalk messages. Also, there is a widely-accepted naming convention used in Smalltalk (it is not a standard that is strictly enforced but it might as well be) which will be presented as well.

The code snippets found in this primer can be "run" or "executed” from a Workspace. To open a Workspace, from the VisualWorks main Launcher window, either click the last button on the Toolbar or select the menu option Tools>>Workspace.Then copy and paste the code from this window into the Workspace.

The 3 types of messages in Smalltalk are:

·  Unary

·  Binary

·  Keyword

Unary Messages

4 squared
'pots' reverse
10 factorial
'chocolate' asUppercase
6.4 rounded

The example statements above represent the basic building block of the Smalltalk language. Just as in English, where the simplest grammatically-correct sentence contains just two words, a noun and a verb ("He jumped." or "She walks."), so too is a Smalltalk expression. It contains an object and a message. They are called Unary Messages because they involve one object and one message.

Binary Messages

3 + 4
6 * 5
'Visual' , 'Works'

The statements above are examples of Binary Messages. Since binary means 2, these expressions involve two objects with a common message. Most binary messages are mathematical in nature, but they do not have to be. The third example ('Visual' , 'Works') involves 2 instances of the String class with the comma as a message. The "comma" method is used for concatenation. In this case, it is used to append the string 'Works' to the receiving object, 'Visual' which forms the resulting String object 'VisualWorks'.

Keyword Messages

Dialog warn: 'Hello World'
Transcript show: 'Hello World'
('Got Milk?') copy replaceFrom: 5 to: 8 with: 'I am lost' startingAt: 6

The statements above are examples of Keyword Messages. The "giveaway" for these types of statements is the colon (:). So in our first example, we use a class called Dialog and send it a message called warn. The colon following the warn message means that something (a parameter) is tagging along with the warn message. In this case, it’s the string of characters enclosed within a pair of single quotes.

Note that in the last example, there are 4 messages (since there are 4 words with colons). This is actually 1 method. The name of the method is replaceFrom:to:with:startingAt:. That may sound a bit strange but it's really quite understandable when you break it down. It is a single method with 4 parameters. The messages themselves describe the parameters the (one) method expects.

Evaluating Smalltalk Expressions

Here are the rules. Remember, no matter how complex an expression, it must eventually break down to

Object Message

  1. Left to right
  2. Sub-expressions within parentheses get done first
  3. Unary messages
  4. Binary messages
  5. Keyword messages

Also, a message always produces an object as a return value. This object can then be used as the receiver of or a parameter for the next message in the expression.


Figure S-1. A typical "mixed" expression.

Since there are no parentheses, work left to right, performing Unary messages first, Binary messages next and Keyword messages last. So you first evaluate

Date today weekday

This would then evaluate to

December 25, 2000 weekday

Of course, the date of "December 25, 2000" would only occur if you were performing this exercise on that given date. Otherwise, it would return the date of the day you try this. So "Date today" returns a Date object. This Date object is then sent the message weekday which returns the day of the week the date occurs.


Figure S-2. How Smalltalk evaluates this "mixed" expression.

Naming Conventions

The book Smalltalk with Style from Skublics, Klimas and Thomas (ISBN: 0-13-165549-3) completely outlines the de facto standard naming conventions used in Smalltalk programming. This primer will highlight some of the more basic ones, at least the ones you need to know for right now.

·  A class name always begins with an uppercase first letter

·  A method name always begins with a lowercase first letter

·  A method name that involves multiple words should never be abbreviated

·  A method name that involves multiple words should use uppercase letters for each word after the first

·  Spaces are not allowed for method names

·  The rules that apply to method names also apply to temporary variable names

Now that these have been identified, it will be easier to work with "mixed" expressions, specifically identifying the different types of methods.

Types of Methods

There are two basic types of methods:

·  Class-side methods

·  Instance-side methods

Let's take a closer look at each type.

Dialog warn: 'Hello World'

Since Dialog is capitalized, Dialog is a class. Since Dialog is a class, warn: is a class-side method. It returns a specific instance of the Dialog class (a dialog box with 'Hello World' on it and an OK button).

4 factorial

The number 4 is an instance of the SmallInteger class. Therefore, factorial is an instance-side method. It returns a specific instance of the SmallInteger class (24).

Date today weekday

Since Date is capitalized, Date is a class. Since Date is a class, today is a class-side method. It returns a specific instance of the Date class (whatever today's date is). Moving left to right, the term weekday is next. Since it follows an instance of the Date class, weekday is an instance-side method. It actually returns a symbol (a symbol in Smalltalk is a special type of object that acts like a constant, but that's not important here). What is important is how the resultant object from "Date today" was used as the receiver of the next message of "weekday".

Transcript show: Date today weekday

Moving left to right, note that we definitely have a keyword message (because of show:) but keyword messages are last on our order list. We must look for unary or binary message first. What happens here is "Date today weekday" will get evaluated first, resulting in "Monday" which is then used as the parameter to the keyword message show: of the Transcript object. It goes something like this:

Transcript show: Date today weekday

Transcript show: (December 25, 2000) weekday

Transcript show: Monday

 

Summary

There is still more to cover on Smalltalk syntax but this will provide enough background on the subject to make Smalltalk code a bit easier to read.