cs2340: Smalltalk 101 - PowerPoint PPT Presentation

1 / 21
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:24
Avg rating:3.0/5.0
Slides: 22
Provided by: joche1
Category:

less

Transcript and Presenter's Notes

Title: cs2340: Smalltalk 101


1
cs2340 Smalltalk 101
  • Fall 2006

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

4
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
5
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

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

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

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

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

10
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

11
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

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

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

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

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

17
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

18
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

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

20
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

21
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