Smalltalk
Tutorial. Precedence, Unary Binary and Keyword Messages, printString,
errors, using parenthesis .
Tutorial 2. Precedence, printString, & errors |
|
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 |
|
Contents
|
Smalltalk PrecedenceYour 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 - 1Enter the above, in the Workspace and evaluate it, and check the answer is as stated in Answer 1. Unary Binary and Keyword MessagesThere 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:
+ * - /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 - 1is 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.
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. 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
plus:message is only available in the M206 version of LearningWorks. Go to Answer 2 Workspace Exercise Check the above answer by evaluating them.
plus:Examination of the method plus: How this is done is introduced later in these tutorials.
|
|
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. 2 plus: 3 * 4 - 1 is evaluated. Workspace Exercise Check the above answers by evaluating 2 plus: 3 * 4 - 1 Error MessagesRead Tip 1. Make a list of Error Messages Then return to this pointEvaluate 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:
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. |
printStringThe 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) printStringin the Workspace TestTest 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).
|
AnswersAnswer 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
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
'5' Note the single quotes.
Test Answers
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. Lets try this now. 2 + 3 printStringand study the Unhandled exception: box that pop up. Click on the cancel button. Change the example to (2 + 3) printStringNow 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 |
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
Copyright © John McGuinn 2000 - 04 |