CS 535 Object-Oriented Programming Fall Semester, 2003 Basic Smalltalk Syntax |
||
---|---|---|
© 2003, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 02-Sep-03 |
Basic Smalltalk Syntax
The Xerox team spent 10 years developing Smalltalk
They thought carefully about the syntax of the language
Smalltalk syntax is
The Rules
Sample Program
"A Sample comment" | a b | a :='this is a string'. ":= is assignment" a := 'this is '' a string that contains a single quote and a newline'. a := 'concat' , 'inate'. a := 5. a := 1 + "comments ignored" 1. b := 2 raisedTo: 5. ^a + b "^ means return"
Multiple Assignments
Assignment statements return values!
| a b | a := b := 3 + 4.
a and b now contain 7
Statement Separator
| cat dog | cat := 5. dog := cat + 2
A period is used as a statement separator
A period is optional after the last statement
Identifiers
An identifier (any name) in Smalltalk is of the form:
| cat dog | cat := 5. dog := cat + 2.
Messages
Most languages place basic operations in the grammar
Three type of Messages
1 + 2 12 / 6
12.3 printString '123' asNumber
'Hi mom' copyFrom: 1 to: 3
'this is a string' reverse 'this is a string' is the receiver reverse is the selector returns 'gnirts a si siht'
anObject->foo(); //C++ anObject.foo(); //Java
anObject foo(); anObject foo();
anObject foo "Smalltalk"
More Unary Examples
25 factorial 25 is the receiver factorial is the selector returns 15511210043330985984000000
'Cat in the hat' size returns 14
12 printString
returns ‘12’ (a string)
‘20’ asNumber
returns 20 (an integer)
Remember the Rules
25 factorial
All receivers are objects
All objects are instances of a class
So
Combining Unary Messages
Unary messages are executed from left to right
100 factorial printString size
is done as:
((100 factorial) printString) size
How about this?
100 factorial size
This will not work
100 factorial returns an integer
Integers do not implement a size method
Use the Smalltalk browser to see the methods in a class
Binary Messages
Format: aReceiver aSelector anArgument
2 + 4 2 is the receiver + is the selector 4 is the argument returns 6
Binary selectors are
+ - / \ * ~ < > = @ % | & ! ? ,
Second character is never -
Combining Binary Messages
Binary messages are executed from left to right
1 + 2 * 3 * 4 + 5 * 6
is executed as
((((1 + 2) * 3) * 4) + 5) * 6
Keyword Messages
Format:
12 min: 6 12 is the receiver min: is a selector with only one keyword 6 is the argument returns 6
One keyword message with two arguments 'this is a string' copyFrom: 1 to: 7
'this is a string' is the receiver copyFrom:to: is one selector with two keywords 1 and 7 are the arguments returns 'this is'
'this is a string'.copy(1, 7); 'this is a string'->copy(1, 7);
One keyword message with four arguments
'this is a string' findString: 'string' startingAt: 4 ignoreCase: true useWildcards: false
'this is a string' is the receiver findString:startingAt:ignoreCase:useWildcards: is one selector ‘string’, 4, true, false are the arguments returns (11 to: 16)
Equivalent in Java/C++-like Syntax
'this is a string'.find('string', 4, true, false); 'this is a string'->find('string', 4, true, false);
Keyword Messages verses Positional Argument Lists
Smalltalk Version
'this is a string' findString: 'string' startingAt: 4 ignoreCase: true useWildcards: false
Where do Keyword Messages End?
Unless you use parenthesis the compiler combines all keywords in a statement into one message
'this is a string' copyFrom: 1 to: 12 min: 7
'this is a string' copyFrom: 1 to: (12 min: 7)
Formatting Keyword Messages
'this is a string' findString: 'string' startingAt: 4 ignoreCase: true useWildcards: false
or
'this is a string' findString: 'string' startingAt: 4 ignoreCase: true useWildcards: false
Beck’s Rule
When a keyword message has two or more keywords
The Tab Verses Spaces Debate
When one indents a line of code do you use:
Precedence
Expression |
Result |
3
+ 4 * 2
|
14 |
3
+ (4 * 2)
|
11
|
5
+ 3 factorial
|
11
|
(5
+ 3) factorial
|
40320
|
'12'
asNumber + 2
|
14
|
'this is a string' reversed findString: ('the cat is white' copyFrom: 9 to: 10) startingAt: 1 + 2 caseSensitive: 2 + 2 = 4
Quiz
Identify the receivers, messages and the order of the messages in the following:
cat cat cat: cat + cat cat: cat / cat
Transcript
Special output window
Similar in purpose to Java's System.out and C++'s out
Useful Transcript messages:
clear clear the Transcript
show: aString display aString in the Transcript
print: anObject display a string representation of anObject in the Transcript
nextPutAll: aString add aString to the display buffer
endEntry put contents of display buffer in Transcript empty the buffer
flush Same as endEntry
tab cr space crtab crtab: anInteger put given character in the display buffer
Sample Program Transcript clear. Transcript show: 'This is a test'. Transcript cr. Transcript show: 'Another line'. Transcript tab. Transcript print: 12.3. Transcript cr. Transcript show: 4 printString. Transcript cr. Transcript show: 'The end'.
Result of Running Program
Cascading Messages
Format:
Transcript clear; show: 'This is a test'; cr; show: 'Another line'; tab; print: 12.3; cr; show: 4 printString; cr; show: 'The end'.
Cascade Versus Compound Messages
Expression |
Result |
'hi
mom' reverse asUppercase
|
'MOM
IH'
|
'hi
mom' reverse; asUppercase
|
'HI
MOM'
|
First send reverse to 'hi mom' The result is 'mom ih' Now send asUppercase to 'mom ih'
First send reverse to 'hi mom' The result is not used Now send asUppercase to 'hi mom'