CPS 343/543 Lecture notes: Introduction to Smalltalk



Coverage: [COPL] Chapter 12, §12.4 (pp. 465-467) and [PLPP] Chapter 10, §10.6 (pp. 446-452)


Smalltalk

  • developed at XEROX PARC in early 1970's by Alan Kay and colleagues as an environment to teach programming to children
  • influenced by both Simula and LISP
  • Smalltalk-72, Smalltalk-76, Smalltalk-80 (simply referred to as Smalltalk)
  • in Smalltalk, everything is an object (not so in C++ or Java)
    • even a class is an object
    • for instance, class FinancialHistory is the only object of class Meta-FinancialHistory
  • each object is an abstraction of a computer
  • was the first pure object-oriented language
    • it is completely consistent with the OO paradigm
    • Ruby is also pure
    • Java is almost pure
  • adopts an anthropomorphic programming model [PLP] p. 491 (i.e., the only way to affect computation is to pass messages to objects which then execute code (a method) in response to the message)
  • not just a language, a complete software development environment
  • requires a mouse which was developed at PARC for this
  • uses a simple, uniform model of the world akin to LISP
  • uses a simple uniform syntax, again, akin to LISP
  • uniformity and simplicity: everything is an object
  • typeless (akin to LISP): any name can be bound to any object; class membership is the only typing
  • supports dynamic polymorphism (i.e., code is associated with no particular type)
  • automatic garbage collection
  • only reference variables which are implicitly dereferenced
  • only supports single inheritance
  • strongly typed (akin to ML and Haskell), but at runtime
  • dynamic nature
    • typeless language (akin to LISP)
    • dynamic binding
    • supports dynamic polymorphism
    • supports reflection: ability to inspect and modify a program while it is running!
  • contributions:
    • a new paradigm (not just a new language)
    • influential in personal computing revolution and windowing systems
  • inspired and pioneered
    • the object-oriented paradigm,
    • the idea of personal computing, and
    • windowing systems and GUI's (mouse)


Smalltalk syntax

  • class names must begin with an UPPER CASE letter and method names begin with a lower case letter; convention in C++ and Java, but not enforced
  • be careful to distinguish between
    • Object: root class (capital O)
    • object: just some object
  • period (.) is the statement separator
  • up arrow is return (typed ^); if omitted, then current object returned
  • left arrow (←) is assignment (typed _)
  • text between single quotes is treated as a string
  • text between double quotes are considered comments
  • local variables introduced with pipe symbols (e.g., | temp |)
  • blocks: [ ... ] can be thought of as a closure and call-by-name parameter; allows Smalltalk to handle control in a purely OO fashion


Smalltalk messages

  • sometimes called selectors
  • messages are written in postfix form
  • a colon precedes the argument to a message
  • (courtesy [PLPP] pp. 447-448)
  • unary
               // C++
               q.front()
    
               "Smalltalk"
               q front
          
  • keyword
               // C++
               q.enqueue(x)
    
               "Smalltalk"
               q enqueue: x
       
               // C++
               table.insert(anItem, anIndex)
    
               "Smalltalk"
               table insert: anItem at: anIndex
          
  • binary: 2 + 3
  • in this manner Smalltalk programs read like English


Purity in Smalltalk

  • 2+3 is not an expression!
  • 2+3 is the message + sent to object 2 with argument object 3, requesting that it add 3 to itself and return the result (the object 5)
  • for loop is an object


Objects in Smalltalk

  • allocated from the heap
  • deallocation is implicit using garbage collection akin to LISP


Simple examples

    1 to: 5 do: [:i | (Transcript show: 'Hello World') cr]
    
    dialog _ FillInTheBlank request: 'How may I help you, Larry?'
    


Developing a Smalltalk program

  • involves extending or modifying the existing class hierarchy
  • can modify the existing class hierarchy (no need to always subclass)
    • increment
    • decrement
  • all user-defined classes are automatically placed into this
  • the root class is called Object
  • life begins at class Object


OOP in Smalltalk

  • class: superclass, instance variables, and methods
  • no privacy control (simplistic/purity) of instance variables or methods, instance variables can only be accessed through methods (i.e., all instance variables private, all methods public)
  • self is the receiver (akin to this in C++ or Java)
  • super is the immediate super class


Use of super for defining new

  • new is a class method
  • classes are objects of a metaclass class (which implements new)
  • new
    ^super new setInitialBalance: 0
  • initialize: amount
    ^super new setInitialBalance: amount


if statement

  • written in postfix form
  • concept of a block
    • statically scoped closures
    • similar to lambda expressions in LISP
  • for instance, (Smalltalk heart) ifTrue: [car honk] (expression is an object) [BIST]
  • value message (evaluates a block)


Applications of Smalltalk

  • simulation
  • research


Comparison of Smalltalk, Java, and C++

What can be done in pure OO languages, such as Smalltalk, which cannot be done with impure OO languages, such as C++ or Java?

conceptSmalltalkJavaC++
purityyesalmostno
dynamic bindingdefaultdefaultas an option
polymorphismsubtypesubtypeparametric
stand alone classnoyesyes
multiple inheritancenonoyes
implicit dereferencingyesyesno
manual dynamic allocationyesyesyes
automatic garbage collectionyesyesno
reflectionyesyesno


Inheritance

  • Simula, Smalltalk, Objective-C, Modula-3, Ada, Oberon support only single inheritance
  • C++, CLOS, and Python support multiple inheritance
  • `Java, C#, and Ruby provide a limited mix-in form of multiple inheritance, in which only one parent class is permitted to have fields' and the other is an interface [PLP] p. 511

For a laundry list of impurities in C++, see [PLP] p 512.


References

    [BIST] T. Budd. A Brief Introduction to Smalltalk. ACM SIGPLAN Notices, 28(3), 367-368, 1993.
    [COPL] R.W. Sebesta. Concepts of Programming Languages. Addison-Wesley, Boston, MA, Sixth edition, 2003.
    [PLP] M.L. Scott. Programming Language Pragmatics. Morgan Kaufmann, Amsterdam, Second edition, 2006.
    [PLPP] K.C. Louden. Programming Languages: Principles and Practice. Brooks/Cole, Pacific Grove, CA, Second edition, 2002.

Return Home