slides for a Brief Overview of Smalltalk

Download Report

Transcript slides for a Brief Overview of Smalltalk

A Smidgen of Smalltalk
CS 331
Principles of Programming
Languages
Overview
• Sethi’s book
– Section 7.7 and 7.8
– Section 15.4
• For an overview of Smalltalk, see Kaehler
and Patterson’s “A Taste of Smalltalk”
• There are numerous Smalltalk resources on
the Web
• We’ll be using Squeak to demonstrate
Fundamental Concepts
• The big idea: everything happens as a
result of messages sent to objects
• Smalltalk code is written in the context of
the Smalltalk development environment
• The Smalltalk system provides lots of
classes which you can use and study
Message keywords
• Class instances are created by class methods
• Most classes understand the “new” message
• Some messages have keyword arguments
which end with a colon, e.g. aStack push:10
• A message may have two or more
keywords, e.g. moveTower fromPin:1
toPin:3
Smalltalk Syntax
• Values to be returned are indicated by ^
– e.g. ^
lastObject
• Conditionals and blocks
– e.g.
x > y ifTrue: [max := x] ifFalse: [max:=y]
• Objects can send themselves messages
– e.g.
Self isEmpty
• The name super accesses an overridden
method
More Syntax
• The character $a, the string ‘foo’
• Assignment operator is the left arrow
– e.g. index <- self find: oldObject.
• Using arrays and array elements
– e.g. A at: I put: x
– e.g. y <- A at: I
– e.g. A <- #(1 2 3)
Example
arrayTest
"See if we can access array elements"
| testVar |
A <- #(1 2 3).
testVar <- A at: 1.
Transcript cr.
Transcript show:('testVar is ', testVar printString).
" (Object new) arrayTest "
testVar is 1