Title: Smalltalk
1Smalltalk
- Date 2009.03.10
- Presenter Young-Jun Lim
2Agenda
- Why Smalltalk?
- History outline
- Environment
- Introducing to the language
- Large-Sacle Features of Smalltalk
- Example
- Alan Kay said
3Why Smalltalk?
- When I invented the term object-oriented I did
not have C in mind. Alan Kay - Pure object-oriented language
- Learn concepts, not syntax very easy to learn
- GC, no memory management, no pointers
- Look at and manipulate objects in real time
- High productivity by short round-trip times
- Easy to understand
- Python, Ruby, Awk, Perl, S,
- Objective C, Java, C,
4History outline
- 1968 SIMULA
- Fisrt object-oriented language
- 1971 Dynabook (Alan Kay)
- easy-to-use computer for anyone
- bitmapped display, pointing device, external
storage - programming creating simulations
- Smalltalk-71
5History outline (cont)
- 1973 Xerox Alto computer
- running Smalltalk (implemented in BASIC)
- iconic characters, turtle graphics (LOGO)
- classes (no hierarchies), instances, self
- 1974
- performance improvements
6History outline (cont)
- 1976
- Smalltalk bytecode interpreter in Alto microcode
- everything is an object
- class hierarchy, super
- code browser, inspector, debugger
- 1978 NoteTaker
- step towards Dynabook portable computer
- cancelled "No one wants portability."
7History outline (cont)
- Smalltalk-80
- 2 goals hardware and community
- language standard
- massively adopts MVC
- forerunner of today's GUIs
8Environment
- Environment
- virtual machine (usually with JIT)
- image snapshot
- export applications as image, deliver with VM
binary - Benefits
- environment is IDE and run-time all the same
- powerful debugging facilities
- on-the-fly implementation modification
9Introducing to the language
- "Pure" object-oriented language
- absolutely everything is an object
- Features
- single inheritance
- dynamically typed
- powerful library and meta-level
- strict class hierarchy (superclass Object)
10Introducing to the language
- Lexical details
- comments double quotes
- characters ltchargt
- strings single quotes
- symbols ltsymgt
- arrays (ltelemgt ltelemgt ...)
- Operators
- assignment
- equality
- identity
"this is a comment" x 'this is a
string' thisIsASymbol ('this' 'is' 'an' 'array'
'with' 7 'elements')
11Expression
- Expression
- Literals (numbers, strings, and keywords)
- Kind of objects
- Keyword is user-defined indentifier
- Variable names (all varialbles are references)
- Private, Shared
- Variable has no type, only refer to object or
class - Message expressions
- Block expressions
12Expression
C/Java
Smalltalk
Transformation t float a Vector
v t-gtrotate(a,v) // for C t.rotate(a,v) //
for Java
t a v aTransformation angle aVector
t.Rotate (a, v) t rotate by a around v
t rotateBy a around v
This is Smalltalk!
13Expression
- Message expressions
- Two parts the receiver object and the message
itself - The message part specifies the method and
possibly some parameters - Replies to message are objects
- Messages can be of three forms
- Unary (no parameters)
- e.g., firstAngle sin (sends a message to the sin
method of the firstAngle object) - Binary (one parameterm an object)
- e.g., 12 17 (sends the message 17 to the
object 12 the object parameter is 17 and the
method is ) - Keyword (use keywords to organize the parameters)
- e.g., firstArray at 1 put 5 (sends the object
1 and 5 to the atput method of the object
firstArray)
14Expression
- Message expressions
- Multiple messages to the same object can be
strung together, separated by semicolons - Ex) outPen homeupgoto500_at_500down, home
- Same meanings outPen home.
- ourPen up.
- ourPen goto
500_at_500. - ourPen down.
15Methods
- General form
- Message_pattern temps statements
- A message pattern is like the formal parameters
of a subprogram - For a unary message, it is just the name
- Ex) currentTotal (return next value)
- (oldTotal newValue) ( mean
return) - For others, it lists keywords and formal names
- Ex) x xCoord y yCoord
- ourPen up goto xCoord_at_yCoord down.
- -gt outPen x300 y400
- Temps are just names
- Smalltalk is typeless!
16Assignments
- Simplest form sum lt- total
- Object (referenced by variable total) referenced
by sum - It is simply a pointer assianment
- Ex) index lt- index 1
- netPay lt- deducts grossPay 350.0
dependents 4
17Blocks
- A sequence of statements, seperated by periods,
delimited by brackets - e.g., index lt- index 1. sum lt- sum index
- A block specifies something, but doesnt do it
- To request the execution of a block, send it the
unary message, value - e.g., value
- If a block is assigned to a variable, it is
evaluated by sending value to that variable - e.g., addIndex lt- sum lt- sum index
-
- addIndex value
18Blocks
- Blocks can have parameters, as in
- x y statements
- e.g., xy sum lt- x10. total lt- sumy
- If a block contains a relational expression, it
returns a Boolean object, true or false
19Conditions
- Conditions
- there are actually no control structures
- all "control structures" are messages
- Messages that can be sent to Boolean objects
- ifTrue, ifFalse
- ifTrueifFalse, ifFalseifTrue
- conditional functionality encapsulated in blocks
if(a lt 0 (b gt c b lt d)) a -c
a negative (b between c and d) ifTrue a
c negated
20Conditions
- How are conditionals implemented?
- class Boolean is abstract
- classes True and False inherit from Boolean
- each has one instance true resp. false
- implementation of ifTrue in class True
- implementation of ifFalse in class True
ifTrue alternativeBlock alternativeBlock
value
ifTrue alternativeBlock nil
21Logical Operations
- Operators implemented in Boolean
- , - eager evaluation (Boolean argument)
- and, or - lazy evaluation (block argument)
- xor, eqv - Boolean argument
- not
This gets executed in any case but nil does not
understand the gt message!
x x nil. x notNil (x gt 0) ifFalse
Smalltalk beep
x x nil. (x notNil and x gt 0 )
ifFalse Smalltalk beep
22Comparing
- Messages implemented in Object
- isNil, notNil
- , - equal, not equal
- , - exactly equal, not exactly equal
- A word about exact equality (, )
- exact equality same object pointer
- do not overload!
- Other examples for comparing messages
- gt, lt, even, odd, ...
23Loops
- Counting loops
- do something 100 times
- Advanced for loops
100 timesRepeat Transcript show 'Hello
world.' cr
1 to 100 do i Transcript show i cr
1 to 100 by 10 do i Transcript show i
cr
100 to 1 by -1 do ...
0.5 to 7.3 by 1.1 do ...
24Loops
- While loops
- blocks can return Boolean values
- messages whileTrue and whileFalse
a a 100 atRandom. a 42 whileFalse
a 100 atRandom
25Classes and Objects
- Classes and objects
- general superclass Object
- there are class and instance variables and
methods - Instantiation
- "constructors" are class methods, e.g., new
- expressive "constructor" names
- possible instantiation method for a Person class
- usage
withName name age age address address (self
new name name age age address address)
withName name age age address address (self
new name name age age address address)
26Classes and Objects
- Visibility
- all instance variables are private
- all messages are public
- instances of the same class cannot access members
- Instance variable access
- convention define two messages of the same name
- instance variable age
- getter message age
- setter message age
age age
age newAge age newAge
27Error Handling
- Dynamic typing caveat unknown messages
- in Object message doesNotUnderstand
- invoked implicitly upon illegal message sends
- override to deal with it
- standard behaviour invoke debugger
doesNotUnderstand aMessage Transcript show
'Do not send me ' show aMessage selector
28Error Handling
- Exceptions
- upon a division by zero, return 1
- Signal exceptions
- superclass of all exceptions Exception
- program errors Error
- notifications that can be ignored Notification
- warnings the user should be aware of Warning
- signal e.g., like this
x / y on ZeroDivide do ex ex resume 1
Error signal 'Something really bad happened.'
29Error Handling
- Return values
- this returns 42 if an error occurs
- return value of ondo
- Resuming
- resumable exceptions (Notification, Warning)
- returns 42, but where the Warning was signalled
Error signal on Error do ex someOp.
42
Warning signal on Warning do ex ex
resume 42
30Error Handling
- Exiting handler blocks
- explicit exit two semantically identical ways
- Retrying
- adjust erroneous values
- use a fallback
Error signal on Error do ex 23
Error signal on Error do ex ex exit
23
x / y on ZeroDivide do ex y
0.000001. ex retry
doSomethingUnsafely on Error do ex ex
retryUsing doItSafely
31Error Handling
- Cleaning up
- place a lock on an external file
- locks must be released even if exceptions occur
- Conditionally cleaning up
- perform cleaning up only if an exception occurred
doLocked aBlock self lock. aBlock ensure
self unlock
someBlock ifCurtailed cleanup
32Large-Scale Features of Smalltalk
- Type Checking and Polymorphism
- All bindings of messages to methods is dynamic
- The process is to search the object to which the
message is sent for the method if not found,
search the superclass, etc. - Because all variables are typeless, methods are
all polymorphic - Inheritance
- All subclasses are subtypes (nothing can be
hidden) - All inheritance is implementation inheritance
- No multiple inheritance
- Methods can be redefined, but the two are not
related
33Alan Kay said
"The best way to predict the future is to invent
it! ".
Alan Kay
34Reference
- ????? ??? ? 4?, Robert W. Sebesta
- Smalltalk Idioms, Aoki Atsushi
- (http//www.sra.co.jp/people/aoki/SmalltalkI
dioms/index_e.htm) - http//www.smalltalk.org/ (Smalltalk Community)
- A Little Smalltalk, Timothy Budd
- Smalltalk with Style, Edward J. Klimas
- Advanced OO Design, Informatik
- Simula and Smalltalk, John Mitchell(Stanford Uni)
- http//www.cincom.com/ (VirtualWorks)
- http//www.squeak.org/(Squeak)
35Question Answer