cs2340: Smalltalk 101 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

cs2340: Smalltalk 101

Description:

Parcels (binary deployment) Store. Changesets Tools- Change List ... shouldNotImplement (refuse to implement an inherited method) ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 21
Provided by: joche1
Category:

less

Transcript and Presenter's Notes

Title: cs2340: Smalltalk 101


1
cs2340 Smalltalk 101
  • Summer 2007

2
Advanced Topics in Smalltalk
  • Loading/Unloading Code
  • Inheritance / Exception Handling
  • File IO
  • Threading
  • Networking

3
Code Import
  • Bundles - Packages
  • FileIn / FileOut
  • Parcels (binary deployment)
  • Store
  • Changesets Tools-gtChange List

See App Developers Guide, Managing Smalltalk
Source Tool Guide, Change Sets
4
Inheritance
  • 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

5
Exceptions
  • 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.

6
New 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).

7
New ANSI Exception Handling
  • Can specify multiple exceptions
  • comma separated
  • handle ArithmeticException, AllocationFailure
  • make an Exception Set
  • specialExceptions ExceptionSet
  • with ArithmeticException
  • with AllocationFailure

8
Create 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.

9
Do 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

10
Do 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

11
Streams
  • Stream metaphor
  • Streams can be on anything, not just files
  • In-Memory ReadStream, WriteStream,
    ReadWriteStream
  • File FileStream
  • Streams have position (next, end, reset)

12
Filename
  • Utilities for accessing files
  • filename bar.xml asFilename.
  • filename Filename named bar.xml.
  • check if file exists
  • filename exists

13
Reading/Writing to File
newFile stream newFile 'testFile'
asFilename. stream newFile writeStream. stream
nextPutAll A text message. stream
close. newFile contentsOfEntireFile
14
Binary 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.

15
XML
  • Classes in XML namespace
  • XML.Document, XML.XMLParser
  • XML.Element, XML.Attribute, XML.Text
  • BasicLibrary Reference

16
Process 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

17
Process 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

18
Synchronization
  • Semaphore class provides thread synchronization
  • sema Semaphore new.
  • sema signal. Up got something
  • sema wait. down wait for something

19
Sharing 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

20
Sleeping or Waiting a specific time period
  • Create a Delay object and call wait
  • d Delay forSeconds 30.
  • d wait.
  • (Delay forSeconds30) wait
Write a Comment
User Comments (0)
About PowerShow.com