Smalltalk Tutorial. Precedence, Unary Binary and Keyword Messages, printString, errors, using parenthesis .

Tutorial 2. Precedence, printString, & errors



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

Smalltalk Precedence

Your answer to the following question will depend on the rules of precedence that you apply. The rules you use will probably depend on your background, such as programming, mathematical, or a calculator user.

 

Question 1. What do you think the result of the following expression will be.
2 + 3 * 4 - 1 

Go to Answer 1

 

Workspace Exercise
2 + 3 * 4 - 1 
Enter the above, in the Workspace and evaluate it, and check the answer is as stated in Answer 1.

Top


Unary Binary and Keyword Messages

There are 3 types of message, unary, binary, and keyword. These were introduced in lesson 1.
Messages are sent to receivers (a receiving object) to make that object do something.
E.g.
afrog colour: Red
2 + 3
myAccount balance

 

The receivers in the above are aFrog, 2, and myAccount.

 

When a Smalltalk expression contains different types of messages, then the rules of precedence that decide the order of evaluation are:
  • Unary messages are sent first. Thus having the highest precedence
  • Then binary messages.
  • Keyword messages are sent last.

 

+ * - / 
i.e. plus, minus, multiply and divide are all binary messages, and all messages within a type have the same level of precedence.

 

Therefore the rule that is used in the evaluation of
2 + 3 * 4 - 1 
is that the evaluation is performed left to right.

 

Message Details Examples
unary Used as follows. receiver unaryMessage
A message that does not have any arguments. E.g. size, asInteger, negated, reversed, asUppercase
Easy to recognise, there are no arguments.
'123' asNumber
45 printString
binary Used as follows. Receiver binaryMessage parameter
Binary messages have a parameter.
Binary messages are used for:-
Arithmetic, e.g. +, -, *, /, //.
Comparisons and logical operations. E.g.=, <, >, <=, >= Think of these messages as asking a question, e.g. is number1 equal to number2.
Easy to recognise, there is an argument and the message selector is not a word, or alpha numeric character.

3 + 2
( number1=number2 )
keyword Used as follows. Receiver binaryMessageColon argument
A keyword message must have 1 or more arguments and is recognisable by the : colon.
Message with 1 argument age:

 

Do NOT place a space between the keyword and its :
e.g. age : is wrong

 

Message with 2 arguments.
Used as follows. Receiver binaryMessageColon argument binaryMessageColon argument
Note how they are written without spaces when talking about the message without the parameters
at:put:
copyFrom:to:
Easy to recognise, there is at least 1 argument. The message selector ends in a colon.
aperson age: 18
aFrog position: 4

 


aCollection detect: [ aBlock ]
Blocks are discussed later

 

 

aCollection at: anIndex put: anObject

 


'abcde' copyFrom: 2 to: 4

Very Important. Make sure you can pick out the

receiver, message selector, and argument in the examples above. They are colour coded to make it easier.


You will very quickly start to struggle in the M206 OU course if you do not pick up the technique of recognising these easily. In the examples given it is not to difficult to pick these out. Later when you get several messages combined on one line it becomes more difficult.

 

Top


Changing Precedence Order

As stated above, where messages are of the same type as in 2 + 3 * 4 - 1 , the order used is left to right.
This order of left to right, can be changed by using parenthesis i.e. ( ) brackets. The expression within the bracket ( is evaluated first) .
Where brackets are nested, the inner most bracket is is evaluated first, then work outwards in towards the outer bracket, and finally the remains of the expression outside the brackets.

 

E.g.

2 + (  (3 * 4) - 1  )
evaluated last (   (evaluated first)  evaluated second  ) 

 

Parenthesis can be used when the brackets are not actually required, to emphasis the actual order. This technique is used a lot in the M206 Open University course.

Check your understanding of these rules by calculating the answers of the following expressions in your head, according to the above rules. Check your answer by evaluating them in the Workspace


Question 2
  1. 2 + (3 * 4) - 1 
  2. 2 + 3 * (4 - 1)
  3. 2 + ((3 * 4) - 1)
  4. 2 + 3 
  5. 2 plus: 3 
NOTE. The
plus:
message is only available in the M206 version of LearningWorks.
Go to Answer 2

 

Workspace Exercise
Check the above answer by evaluating them.

 

Top


plus:

Examination of the method plus: How this is done is introduced later in these tutorials.

plus: aNumber

^self + aNumber


plus: aNumber

Method heading consisting of the name + and argument aNumber
The method body No helpful comment.

^self + aNumber

The code for the method. Again as in the + method you have the caret ^ character, self and the argument aNumber.
But have you noticed that the message + is being sent to self in the code.

 

The two methods are doing the same.

You already know that

2 + 3 * 4 - 1 

answers with 19 and that + and plus: do the same calculation.
Question 3 What answer will be returned if

2 plus: 3 * 4 - 1 

is evaluated.
Question 4 Explain this answer.
Go to Answer 3 and 4

 

Workspace Exercise
Check the above answers by evaluating

2 plus: 3 * 4 - 1

Top


Error Messages

Read Tip 1. Make a list of Error Messages Then return to this point

 

Evaluate 2 +
Because + is a binary message selector, and thus should be followed by an argument, an error message appears as follows:-
2 + Argument expected->

 

Before pressing the enter key to delete the highlighted error message, note the following:
  • The error message produced.
  • The position of the error message, within your expression.
  • Add this message to your own, list of error messages.
  • Add how you corrected the error, to your list.

When you have deleted the a highlighted error message, enter the argument e.g. 3 so that the expression reads

 

When you checked out the list of error messages did you notice that this exception message can be 2 + 3
Workspace Exercise
Evaluate to ensure this works.

Strong recommendation: If you have not started to make out a list of error messages. Start one now.

Top

printString

The method comment for printString is "Answer a String whose characters are a description of the receiver."
In Smalltalk a string starts and ends with a single quote, E.g. 'Hello World' , '1234' etc.

 

Example Usage 4 printString
would answer with '4' which you can use if required.
When printString is sent to an object a string is returned.

 


Question 5 Is printString a unary, binary or keyword message.
Go to Answer 5

 


Question 6 What do you think (2 + 3) printString will evaluate to?
Go to Answer 6

 


Workspace Exercise
Evaluate
(2 + 3) printString
in the Workspace

Top


Test


Test Question
(a). List the 3 types of message in the order of precedence in which they are carried out.
(b). What can you use to force a change in the order of precedence?
(c). What is the general rule that is used for the order of precedence?
(d). Which message type is very easy to recognise because it has a colon: in it?
(e). Which 2 types of message always have an argument?
(f). Which message can have 1 or more arguments?
(g). Fill in the missing word. A unary message is easy to recognise because it does NOT have an ......
(h). What message type is printString?
(i). If the evaluation of 5 printString Answers with '5' . What do you think the result of 2 + 3 printString will be, and why?

 

Workspace Exercise

 

When you have read the answer to (i).
  • If you have not evaluated 2 + 3 printString . do it now.
  • Add this exception message to a list of these messages that it is advisable to create.
  • Then correct the expression to avoid the unhandled exception message.
  • The cause of this, is a very common error.
  • Always work out what message is being sent to which receiver.
  • Mastering the Smalltalk precedence rules is essential.
  • If you are struggling with correcting the expression 2 + 3 printString so that it works correctly

Top


Answers

 

Answer 1
If you are a mathematician or have programmed in other languages you may think that the answer is 13, because usually
* and / have a higher precedence than + and - .

 

This is NOT the case with Smalltalk.
Evaluation will show that the expression 2 + 3 * 4 - 1 answers 19 when evaluated.
Smalltalk's precedence rules are different.
Return to Question 1

 




Answer 2
(a). 2 + (3 * 4) - 1 answers 13
(b). 2 + 3 * (4 - 1) answers 15
(c). 2 + ((3 * 4) - 1) answers 13
(d). 2 + 3 answers 5
(e). 2 plus: 3 answers 5

 


Return to Question 2


 


Answer 3
2 plus: 3 * 4 - 1 answers with 13
(Note this is a corrected answer from earlier versions, where the answer order had got out of sink with the questions)

 

Answer 4
2 plus: 3 * 4 - 1 contains a mix of keyword:- plus:, and binary:- *,-, message selectors.
Binary takes precedence over keyword selectors.
The 2 binary selectors evaluate left to right. If parenthesis are added to emphasis the order we get.
2 plus: ( (3 * 4) - 1 )
Doing the calculation in order we get these stages.
2 plus: ( (12) - 1 )
2 plus: ( 11 )
13

 

Return to Question 4

 



 


Answer 5
printString is a unary message.
Return to Question 5

 



Answer 6

  '5' 

Note the single quotes.
Return to Question 6

 

Top


Test Answers

  1. Unary, binary and keyword.
  2. Use parenthesis.
  3. Left to right.
  4. Keyword.
  5. Binary and keyword.
  6. Keyword.
  7. Argument.
  8. Unary
  9. The result is an error exception message, Unhandled exception: Message not understood #sumFromInteger:
printString is a unary message and has a higher precedence than the + binary message. Therefore the first expression evaluated is 3 printString . This produces an answer of '3' a string.
The expression now would look like 2 + '3'
A string can not be used as an argument to the message +

 

Lets try this now.
Evaluate

2 + 3 printString 
and study the Unhandled exception: box that pop up.
Click on the cancel button. Change the example to
(2 + 3) printString 
Now work out why this works correctly and answers '5'.
Its all to do with the precedence rules.

 

Return to Test questions

 


| Home Page | Previous Tutorial 1.| Next Tutorial3. Strings | Top of Page |

 

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