Smalltalk tutorial. Terminology. Simple message expressions. Using LearningWorks. Smalltalk Programming.

Tutorial 1. Starting Smalltalk and Terminology



More Tutorials
C Programming Tutorial
T223

HTML Tutorial & Web Design
T170,T171 and TT180

Search this site

Home

 

Tutorials
| 1. Starting Smalltalk, and Terminology | 2. Precedence Rules | 3. Classes - Strings | 4. Class Browser | 5. Variables | 6. Error Messages | 12. Control Structures | 16. Dialogs | 23. Collection class | 39. OpenGUI |

 

Information
| Smalltalk Books | Student Software & Books | Hints & Tips | Sites | Download Smalltalk | Coloured code | Download Documents | ASCII Codes |

 

Smalltalk Index

 

 

 

Student
Licence Software
at special prices

 


Contents


Top

Tutorial 1. Aims of the lesson

  1. The main aim of this Smalltalk lesson is to look at some of the basic Smalltalk terminology. M206 Open University (OU) students should try to understand this terminology as soon as possible.
  2. To start the Smalltalk program.
  3. How to enter a simple message expression 2 + 3 .
  4. How to evaluate this expression.
  5. How to look at the result and what has happened.
  6. How to examine the method +.

Top


The lesson

Terminology. The OU M206 course is full of terminology. The quicker you learn this terminology the easier the course will be. This exercise introduces the following main terms:-

answers, argument, expression, method, method heading, method body, message, message expression, object, receiver.

Try to understand this terminology as you read the lesson, and the the glossary at the foot of this page,

Golden Rule 1.
In Smalltalk a Smalltalk object is made to do something by sending a message to that object.
This takes the form of object message

A very simple rule that

  • First write down the name of the Smalltalk object.
  • Then after the name write the message.
  • The Smalltalk object receiving the message is called the receiver object or simply receiver.
  • There are 3 types of messages that can be sent to a receiver object. Unary messages, Binary messages or Keyword messages messages.

Example. This could be likened to a drill instructor getting a squad of soldiers to do something by issuing commands. In the following example many of the terms are given. At this stage try to obtain an overall picture of the use of these terms.


Top

Example of objects sending and receiving a message.

An army drill instructor commands his B squadron to right turn by shouting "B Squad right turn". Similar other commands are left turn and about turn.

In Smalltalk this command could be written as bSquad turn: right.

Don't worry if you do not understand the following sentence. Assuming that a method named turn: has been written and that bSquad is the name of an instance of class Squadron, that understands the message turn: right.

bSquad is the receiver of the message turn: right.

turn: right is the message . Made up of a keyword turn: and an argument right.

Turn: is also a message selector. This means that there is a method named turn: and because turn:ends with a : it is a keyword. It also means that turn:must have an argument. Expressions within the method would deal with the different acceptable arguments, such as right, left and about. The method turn: would return an answer, this may be the default answer, the receiver i.e. bSquad, or 1 of the 3 arguments, i.e. directions that are acceptable to the method, or suitable error message to the user if an unacceptable argument is sent.

right is 1 of 3 acceptable arguments for the keyword turn:.


Top

Practical 1

You will require one of the Smalltalk programs listed in table 1, on your computer.

M206 LearningWorks is the one provided by the Open University for students taking M206 Computing an Object-oriented approach.

The others were available as a free download. See the Download free versions of Smalltalk page.

Start your Smalltalk Program. Follow the instructions for your version of Smalltalk on how to start the program. You then need to find the window to use as per table 1 below.

Table 1.

Version Window(s) to use
M206 LearningWorks Workspace window
See Using M206 LearningWorks
LearningWorks "Expression evaluation" tab.
See Using LearningWorks
Dolphin Dolphin Work space or System Transcript window
Smalltalk Express Transcript window or using New Window from the Window menu will open a Work space window.
VisualWorks  

Top

Entering a message expression.

Depending on which of Smalltalk version you are using. Click on the correct window, as per table 1, to select it. If there is writing in the window scroll to the bottom and Click after the writing.

 

Exercise T1-1. Enter the expression into the window by typing 2 + 3

Evaluate this expression.

Follow the instructions in the table below, for your Smalltalkversion on how to evaluate expressions entered into the window.

Table 2

Version How to evaluate the selected expression.
M206 LearningWorks Highlight (drag the mouse over) the expression. Then click the "Evaluate Selection" button
LearningWorks Highlight (drag the mouse over) the expression in the "Expression pane". Then click the "evaluate" button
Dolphin Place cursor somewhere in the required line. Then click the "Display It" icon. Or use Ctrl D
Smalltalk Express Highlight (drag the mouse over) the expression. Then select "Show it" from the Smalltalk menu
VisualWorks Not tried this.

The answer 5 should be displayed

Where the answer is displayed will vary depending on the Version used. If using M206 LearningWorks the answer will be in the Answer Pane.

In Smalltalk messages are sent to objects

The message + 3 , is sent to the receiving object 2 , which is called the receiver

 

2 is the method selector, which means that + is the name of the message, and refers to a method with the same name. The method contains the Expressions that is executed when the message is sent to the receiver

 

+ is a Binary method. More on this later

Top

The following Workspace animation shows the above process for M206 LearningWorks

 

{Animated image of Workspace}

Top

Smalltalk Methods

There are 1000's of already written Smalltalk methods that are classified into different groups called Classes. There are 7 slightly different + methods depending on the Class of the receiver. Because the receiver 2 is an integer, then the + method in the integer Class is automatically used by Smalltalk. Classes are introduced in chapter 6 of the OU course.

These methods can be examined using a Class browser. This is done later in the course. If +is looked up using the Class browser the following is seen.

+ aNumber

 

"Answer the sum of the receiver and the argument, aNumber."

^aNumber sumFromInteger:self

Examination of the method. NOTE: Athough a brief description of the method and its Expressions,is given below. At this stage it is not necessary for you to understand how the code works. Understanding takes time. What is important is to be able to understand the comment in the method, when a comment is available. The comment is the "wording within the double quotes". As you progress through the course you will write or amend methods.


Assume 2 + 3 is being evaluated. Remember that
  • 2 is the receiver
  • + is the message selector which refers to the method you are going to look at. The method contains the code.
  • 3 is the argument.
  • + 3 is the message consisting of the message selector + , and the argument 3 .

Top

Now for an explanation of the method.
Section of Method Explanation
+aNumber Method heading consisting of the name + and argument aNumber which in our example will refer to 3
"Answer the sum of the receiver and the argument, aNumber."

The method body consists of the comment and the code.

A descriptive comment, stating what the method does, not how it does it..

Comments are always within double quotes, and are disregarded by the compiler.

As you continue with the OU course, it is important to read these comments. It may take a while before the style in which they are written, becomes easily readable.

Answer is the term used in Smalltalk, to say what will be returned by the method or expression

In plain English this is saying "Return an answer (which will be 5) after adding 2 and 3
^aNumber sumFromInteger:self The code for the method. I do not expect you to understand this code in this lesson.
But note.
  • The caret character ^. This indicates that a specific answer will be returned to the receiver. The answer that will be returned follows the ^
  • aNumber is a variable (variables are covered later) and refers to the argument, 3 in our example. See that it appears more than once. In the heading, and where it is actually used in the method.
  • sumFromInteger: is another method. Which in turn evaluates even more methods.
  • self refers to the receiver, which is 2 in our example.
  • self is the argument to the keyword selector, sumFromInteger: which like all message selectors refers to a method.
  • If you replace the value of the variables used you have ^3 sumFromInteger: 2
    • This expression in turn is evaluated and you would need to look at the method sumFromInteger: to see how this is done.

Top

Remember

Note. Usually in TMAs when you are asked to supply code, a comment is also required.

What the code does.

The value of the comment is of immense importance. It states what the method does rather how it does it.

Top

Simple Message

Of course, an object can do nothing by itself. In Smalltalk, you send messages to objects to make things happen. Messages are similar to function calls in other languages. For example, look at this Smalltalk expression, composed of a single message:

20 factorial

If evaluated in the workspace. The answer would be displayed in the answer pane and would be a very large integer:

2432902008176640000

A simple message expression that can be evaluated, can be of the following types

  1. Examples of a receiver object
    • afrog
    • 69
    • 'Hello World'

     

  2. Examples of a receiver object and unary message selector
    • 20
    • 'now is the time' size

     

  3. Examples of a receiver object and binary message selector and one arguments.
    • 6 * 4
    • variable1 + variable2

     

  4. Examples of a receiver object and keyword message selector and one or more arguments.
    • aFrog position: 4

Far more complex expressions can be also be evaluated. You will be doing this in Smalltalk tutorial 6.

Top

 

Glossary / terminology

This Glossary is quite heavy reading, but it contains many of the main key words that are essencial for you to understand. This understanding will take some time to aquire. You should re-read this Smalltalk tutorial and this glossary from time to time, as the principles of Smalltalk programming in sink in.

 

The main principle of Smalltalk is very simple ...
You send a message to a receiving object, to make that object do something.

Note: The message causes a method of the same name to be executed.

Term Meaning
Answers See Message answer
Argument An argument contains additional information that Binary and Keywordmessages require. Unary messages do NOT have arguments. The argument is past to the method that corresponds to the message.
A variable in the header of the method will refer to this argument.

Workspace example...

anAccount balance: 100 
anAccount is the receiver.
balance: is a keyword message selector.
balance: 100 is the message.
100 is the argument.

The method balance:

balance: anAmount
         "Set balance of receiver to anAmount (a number). 
           Answer the receiver."
           balance := anAmount
See method in this Glossary for more information on this method
Binary message The selector in a binary message is composed of one or two non-alphanumeric characters.
Examples: Arithmetic Binary selectors + - * / . E.g. 2 + 3
Comparison selectors=<==<=> >=E.g. ( aNumber < anotherNumber )
The , (comma) character is binary selector used to concatenate(join together) objects . An example of the concatenate message with string literal objects is
'Hello', ' World'
Which would answer with the string literal 'Hello World'.
See Keyword and Unary
Class A class describes the behaviour of a set of objects of the same kind
Comment Commentsare ignored by Smalltalk and are not executed. Comments are enclosed in double quotes. A comment can be placed anywhere in the program code but the initial comment should describe what the method does.
Other comments can be used to explain how code works.
Evaluate An expression can be evaluated i.e. compiled and executed. An answer is returned.
Expression See Message expression.
Getter See getter message and getter method.
Keyword message A keyword is a selector with one or more arguments. The keyword always ends with a colon, there is no space between the keyword and the colon. The colon is followed by a space, then an argument. E.g. with a single argument plus: 3
at:put: is an example of a keyword requiring 2 arguments. E.g. 'ABDDE' at: 3 put: $C. The arguments are 3 and $C.

 

Note when referring to the selector at:put: there are no spaces. How in use (see example) the keyword selector, at:put:, is split up with a space and argument following each colon 'ABDDE' at: 3 put: $C.
See also Binary and Unary messages. Tutorial 3. at:put:

Top

Message
  • A message is sent to an object to make the object do something.
  • The receiving objects does this by executing one of its methods
    • The method has the same name as the message.
    • E.g. If the message left is sent to an instance of a Frog then the method left in the protocol of Frog is executed.
  • Every message in a classes protocol, has a corresponding method of the same name
Because messages and methods have the same name, learn as early as possible to recognise the difference. The difference is where and how the name is used.

 

The message printstring
  • Refers to its corresponding method. Think of the message such as printString as selecting " the method printString, and I want the code in that method evaluated ."
  • At the start of the course you are writting messages in the Workspace. In the Workspace the name used is always a message, never a method.
    • In some other Smalltalk versions the Workspace is known as a transcript window.
  • Later in the course when you start writting your own methods, you will incorporate messages within those methods. Messages that in the workspace are preceded by objects such as frog1, myaccount, ahoverfrog are now preceded by a psedo variable self or super.
Getter and Setter Methods Objects have instance variables (Called attributes in the early weeks of the M206 course). These instance variables contain information about the object.
Class Instance variable (attribute)
Frog colour, position
Account balance, holder, overlimit


The 5 instance variables in the above table have getter and setter methods to either obtain (get), or change (set) the value in the instance variable. These special methods are often known as assessor pair methods.

The names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:

 

Getter Methods

Getter message or get message, asks for information. E.g.
frog1 colour
The getter message colour is asking frog1 "what is your colour"

refer to the corresponding getter method.

Getter message

A getter method or get methodis one of the 2 assessor pair methods.

Getter methods

  • Provide (get) information
  • return the current value of its corresponding attribute (instance variable).
  • E.g.. aHoverFrog height, would answer with the the value of the attribute height.

The English equivalent is "This is a question to aHoverfrog, What is your current height ?"

Setter Message

Setter message or set method, refer to the corresponding setter method. The setter method always has a colon after the name, this colon is followed by a space then a parameter

E.g. Frog1 position: 2

The English equivalent is "Frog1 make position 2 you current position"

Setter method A setter method or set method is the other of the 2 assessor pair methods.
  • Setter methods set the value of an attribute, by assigning a value to the attribute.
  • This causes a change in an object's state.
  • They usually return the receiver object.
  • E.g. Evaluating aHoverFrog height: 3 message expression in the WorkSpace, would cause the setting of the attribute height of aHoverFrog by the method height:, which after setting the value of the attribute height to 3, would answer with aHoverFrog. The textual representation of the answer would appear in the WorkSpace answer pane.

Top

Message answer When a message is sent to a receiving object, an answer is always returned. The message answer may or may not be used. If an answer is not explicitly coded in the method using the caret ^, then the default answer is the receiving object.
Suitable message answer object may be used subsequently as the receiver or argument of another message.
Message expression
  • A message expression consists of a receiver, and a message. It is often abbreviated to 'expression'. The message may require an argument
  • Messages consist of a message selector, and zero or more arguments.
  • A message expression can be typed in and evaluated in the Workspace, or can be found in the code of a method.
Message selector Selector is the word given to the name of a message.
  • The selector for the expression aFrog left is left a unary message selector.
  • The selector for the expression 'ABDDE' at: 3 put: $C is at:put:
    • This is an example of a keyword requiring 2 arguments. E.g. 'ABDDE' at: 3 put: $C. The arguments are 3 and $C.
  • Message selectors refer to a method with the same name , that will be evaluated, when the message is evaluated. You can think of the message selectors selecting the method
  • The receiver must be able to understand the message, that means the method must be in the protocol of the receiver. (Covered later).
Method A method contains the code that is executed when the message with the corresponding name is sent to an object.

 

  • .
  • A method is evaluated when a receiver is sent a message of the same name.
  • Methods can be viewed, created and writen, and some can be amended in the class browser. (Tutorial 5)
  • A method consists of:-
    • The heading, containing the method name and if required the argument(s).
    • The body containing the "comment" in double quotes
    • The code

Workspace example...

See Argument in this Glossary for information on the following line
anAccount balance: 100 
The message balace: 100 has been sent to the receiving object anAccount.
This message is saying in very plain English,"Make the balance of anAccount equal 100".
In Smalltalk jargon this would be "Set the value of the attribute (instance variable) balance, of the object refered to by anAccount, to 100."

Lets look at the method balance:

balance: anAmount
         "Set balance of receiver to anAmount (a number). 
           Answer the receiver."
           balance := anAmount
  • balance: is a keyword method, the : (colon) indicates this.
    • Keyword methods always have at least 1 argument, anAmount, in the above.
  • balance: is a setter method, its directly sets the value of the attribute (instance variable) balance.

Examination of this method...
Section of the method Comments on that section

balance: anAmount
The heading
balance: 
Method name
                  anAmount
A variable to refer to the argument
"Set balance of receiver to anAmount (a number). 
  Answer the receiver."
The comment.
	balance := anAmount
The code that does all the work.
	balance
An attribute (instance variable) that is directly assigned the value of the variable anAmount.
	                 := 
The assignment operator
	                        anAmount
The argument variable, whos value is 100 in our example

Top

Object

Everything in Smalltalk is an object. As long as you remember this a sentence may make more sense if, in your mind, you remove the majority of occurances of the word 'object' from that sentance.

E.g.
A message object is sent to a receiver object, which will result in an answer object being returned.

The above is easier to read as:
A message is sent to a receiver, which will result in an answer being returned.

The Open universities course M206 in emphasising the point that every thing in Smalltalk is an object over uses the word to an extent that it almost confuses the student.

  • Think of an object as almost anything in the world.
  • Think of a car as an object, for example.
    • A car has certain properties or attributes, like its make, model, colour, engine size, etc.
    • A car has methods for doing things e.g. it can move forward, reverse, accelerate, and sound a horn, etc.
  • Smalltalk is an object-oriented language and everything in Smalltalk is an object.

Smalltalk
  • Objects are the basic building blocks of the Smalltalk language.
  • Objects have a unique identity (name).
  • Objects communicate with other objects by sending and receiving messages.
  • There is a class hierarchy of these object, that look like the roots of a tree. This hierarchy is depicted in the Class list box of the class browser. (Tutorial 5) by the degree of indentation
    • The top-level class is called called Object.
    • Every Smalltalk class is either a subclass of Object or may be further down the roots of the tree being a subclass of a subclass etc.
  • Every object has a textual representation associated with it.
    • Textual representations of message answers may be...
      • Uninformative. an Account for Account class,
      • More informative An instance of class Frog (position 1, colour Green) for Frog class
    • The textual representation can be a default one as in the Account class, or can be coded within a special method for the Frog class.
    • During the M206 course you will do exercises that add additional attributes (instance variables) to a method, say the age of a frog. The code that changes the textual representation also requires to be changed but this is fairly advanced coding and you will not be asked to change this. Therefore the faulty textual representation will not display the additional age attribute. By this time you will have learnt how to inspect an object, and inspection will provide information both on the original attributes and the new attribute(s) you have created in the exercise.

     

The Open universities course M206 in emphasising the point that every thing in Smalltalk is an object over uses the word to an extent that it almost confuses the student.

Tip:
Everything in Smalltalk is an object.
As long as you remember this a sentence may make more sense if, in your mind, you remove the majority of occurances of the word 'object' from that sentance.

E.g.
A message object is sent to a receiver object, which will result in an answer object being returned, emphasising that everything in Smalltalk is an object.

The above is easier to read as:
A message is sent to a receiver, which will result in an answer being returned, emphasising that everything in Smalltalk is an object.

 

Protocol The set of messages an object understands.
Each object responds to a particular set of messages (its protocol).
Receiver The object which receives a message. Also known as the receiver object.
anAccount balance: 100 
In the above expression anAccount is the receiver of message balance: 100
Setter See set message and set method.
Unary message Unary messages do NOT have arguments. Examples of Unary expressions
123 printString
'123' asNumber
See Binary and Keyword messages

 

| Home | Next Tutorial 2. Precedence | Top

 

Top of Page.

 

Sites by John McGuinn . HTML and Web Design | C Programming

Leeds & the UK Tourist Information & guide my home town

Relax in the sun.

Benidorm   Tenerife   San Marino apartments to rent

Holidays and Short Breaks     Manchester Airport

Leeds my home town City of Leeds information  Leeds and Bradford International Airport

AireWeb Web Design
Leeds Web Design Web Designers Hosting Domain names Web site Promotion

Copyright © John McGuinn 2000 - 04