CSE 341 -- Smalltalk Intro
Background
The Learning Research Group at Xerox Palo Alto Research Center (PARC)
developed Smalltalk as a language for the Dynabook, during the 70s and
early 80s. The Dynabook was a yet-to-be-developed "laptop of the future."
Alan Kay, Dan Ingalls, Adele Goldberg were key players in the development
of Smalltalk. They selected simulation as the metaphor for programming
the Dynabook. Smalltalk was influenced by Lisp, Simula, Ivan Sutherland's
Sketchpad.
Characteristics of Smalltalk-72:
- interpreted
- extensible syntax:
fred move up x inches
- more object-oriented than Simula -- even integers are objects
- 'class' is an object (but a rather special one). No subclassing
Characteristics of Smalltalk-80 (now also known just as Smalltalk):
- everything is an object, even classes, blocks (closures), activation
records, etc.
- classes can have subclasses
- not statically typed
- rich programming environment
- definitely no longer a programming language for kids!
Smalltalk is now a commercial product (ParcPlace/Digitalk, Object
Technology International, IBM Smalltalk). It has been a hot item on Wall
Street and the MIS world ... although it may be eclipsed by Java in the
future.
From the point of view of studying programming languages, it is an
excellent example of a pure object-oriented language.
Smalltalk influenced the development of other object-oriented languages,
such as C++, Objective C, CLOS, Java, and our own Cecil language, as well
as others.
Language Overview
We'll now give a quick overview of the language, via a few small examples.
Basic concepts:
- objects
- instances
- classes
- messages and methods
Syntax:
- unary messages
examples: "new", "copy"
Date today Time now hours
Array new someCollection copy
- keyword messages
examples: new:, at:, at: put:
Array new: 10
someArray at: 1 put: 54
anArray at: 1
- binary messages -
examples: + - * /
5 * 9, 3 + 2 * 5
Precedence:
- unary>binary>keyword, left-to-right within same kind,
- parentheses override
- Question: What is the value of 3 + 2 * 5 ?
25, NOT 13
Use parens to get what you want: 3 + (2 * 5)
Note that we will very frequently be composing messages -- for example
Time now hours + 1
first sends the message now
to the class Time
,
which returns the current time (an instance of Time). We then send this
object the message hours
, which returns an integer. Then we
send the message +
with the argument 1 to this integer,
returning another integer (which will be the current hour plus 1).
Example 1: Stack
First we define a new class. See p 54 of the Winston book for how to do
this in the Smalltalk environment. Also see p 171 for how to save the code
in a file and read it back in again.
Object subclass: #Stack
instanceVariableNames: 'anArray top'
classVariableNames: ''
poolDictionaries: ''
Now define some methods:
push: item
top := top+1.
anArray at: top put: item
pop | item |
item := anArray at: top.
top := top-1.
^ item
setsize: n
anArray := Array new: n.
top := 0.
Some code to test the stack:
S := Stack new.
S setsize: 10.
S inspect.
S push: 'hi there'.
S push: 3.14159.
S pop
Adding error checking and growing:
push: item
| save |
top := top+1.
top > anArray size ifTrue:
"anArray is about to overflow. make a new array twice as big, and
copy the old values into it"
[save := anArray.
anArray := Array new: 2*save size.
1 to: save size do:
[:k | anArray at: k put: (save at: k)]].
anArray at: top put: item
pop | item |
self isEmpty ifTrue: [self error: 'trying to pop an empty stack'].
item := anArray at: top.
top := top-1.
^ item
isEmpty
^ top=0