Title: cs2340: Smalltalk 101
1cs2340 Smalltalk 101
2Sudoku Demo
3Advanced Topics in Smalltalk
- Loading/Unloading Code
- Inheritance / Exception Handling
- File IO
- Threading
- Networking
4Code Import
- Bundles - Packages
- FileIn / FileOut
- Parcels (binary deployment)
- Store
- Changesets Tools-gtChange List
See App Developers Guide, Managing Smalltalk
Source Tool Guide, Change Sets
5Inheritance
- shouldNotImplement (refuse to implement an
inherited method) - subclassResponsibility (force subclasses to
implement this method) - These are not reserve words, they are messages to
instances of class Object
6Exceptions
- error a string.
- Old exception handling mechanism common to all
objects (like try/catch) - handle do sent to instance of Signal
- ArithmeticValue divisionByZeroSignal
- handle ex Transcript cr show ex
errorString - do 4 // 0
- Throwing DivisionSignal raise.
7New ANSI Exception Handling
- Replaces Signal with Exception class hierarchy
- send the on do message to a block
- x y
- x 7.
- y 0.
- x / y
- on ZeroDivide
- do ex Transcript show 'zero divide
detected' cr. - Avoid grabbing all exceptions/errors
- on Exception or on Error
- Method will return the result of exception block
(in above example, the Transcript).
8New ANSI Exception Handling
- Can specify multiple exceptions
- comma separated
- handle ArithmeticException, AllocationFailure
- make an Exception Set
- specialExceptions ExceptionSet
- with ArithmeticException
- with AllocationFailure
9Create New Custom Exceptions
- Subclass Exception or Error
- Throwing the exception
- MyException raiseSignal
- MyException raiseSignal OOPS
- the string OOPS can be retrieved by sending
the description message to the exception.
10Do Overs??
- What if I want to retry something with a new
value? just sent retry to the exception
instance. - x / y
- on ZeroDivide
- do exception
- "make the divisor very small but gt
0" - y 0.00000001.
- exception retry
11Do Over (but try something new)!
- I want to try again, but use a different
algorithm - self doTaskQuickly
- on LowMemory
- do exception
- exception retryUsing self
doTaskEfficiently - Note Fail a second time passes exception up chain
12Streams
- Stream metaphor
- Streams can be on anything, not just files
- In-Memory ReadStream, WriteStream,
ReadWriteStream - File FileStream
- Streams have position (next, end, reset)
13Filename
- Utilities for accessing files
- filename bar.xml asFilename.
- filename Filename named bar.xml.
- check if file exists
- filename exists
14Writing to File
newFile stream newFile 'testFile'
asFilename. stream newFile writeStream. stream
nextPutAll A text message. stream
close. newFile contentsOfEntireFile
15Binary Storage of Objects (BOSS)
- colorNames newColor bos
- "First create a file containing color names."
- colorNames ColorValue constantNames.
- bos BinaryObjectStorage
- onNew 'colors.b' asFilename writeStream.
- bos nextPutAll colorNames
- ensure bos close.
- "Then append a new color name."
- newColor mudBrown.
- bos BinaryObjectStorage
- onOld 'colors.b' asFilename readAppendStream.
- bos setToEnd.
- bos nextPut newColor
- ensure bos close.
16XML
- Classes in XML namespace
- XML.Document, XML.XMLParser
- XML.Element, XML.Attribute, XML.Text
- BasicLibrary Reference
17Process Control
- Creating a new thread (or lightweight process) in
Smalltalk is easy - send fork message to a block. to start
immediately - send newProcess message to a block to create but
not schedule the process. - Creates instance of the Process class
- Valid states Suspended, Waiting, Runnable,
Running
18Process Control
- To pass arguments use
- proc index emp code
newProcessWithArguments (2 employee a). - proc resume. start it up
- proc suspend. stop till I say otherwise
- proc terminate. die you monster
- proc yield. give up the rest of my time
19Synchronization
- Semaphore class provides thread synchronization
- sema Semaphore new.
-
- sema signal. Up got something
- sema wait. down wait for something
20Sharing Data
- SharedQueue class provides for thread-safe data
transfer. - q SharedQueue new.
- q next. get next item or wait till item
available - q nextPut a. put an item in queue, folks
waiting will be notified
21Sleeping or Waiting a specific time period
- Create a Delay object and call wait
- d Delay forSeconds 30.
- d wait.
- (Delay forSeconds30) wait