Smalltalk Shopping
Smalltalk
Website Links For
Smalltalk
 

Information About

Smalltalk





HISTORY

Smalltalk was invented by a group of researchers led by Alan Kay at Xerox Palo Alto Research Center; Alan Kay designed the system, which Dan Ingalls implemented. The first implementation, known as Smalltalk-71, was created in a few mornings on a bet that a programming language based on the idea of message passing inspired by Simula could be implemented in "a page of code". A later version actually used for research work is now known as Smalltalk-72. Its syntax and execution model were very different from modern Smalltalk, so much so that it could be considered a different language.

After significant revisions which froze some aspects of executional semantics to gain performance, the version known as Smalltalk-76 was created. This version added inheritance, featured syntax much closer to Smalltalk-80, and had a development environment featuring most of the tools now familiar to Smalltalkers.

Smalltalk-80 added metaclasses, something which helps keep the "everything is an object" statement true by associating properties and behavior with individual classes (for example, to support different ways of creating instances). Smalltalk-80 was the first version made available outside of PARC, first as Smalltalk-80 Version 1, given to a small number of companies ( Hewlett-Packard , Apple Computer , Tektronix , and DEC ) and universities ( UC Berkeley ) for "peer review" and implementation on their platforms. Later (in 1983) a general availability implementation, known as Smalltalk-80 Version 2, was released as an image (platform-independent file with object definitions) and a virtual machine specification.

Two of the currently popular Smalltalk implementations are descendants of those original Smalltalk-80 images. later ported Hobbes to Squeak).

IBM has indicated that VisualAge Smalltalk will be supported by partner company (and VisualAge implementors) Instantiations. Cincom, Object Arts, GemStone, and other vendors continue to sell Smalltalk environments. The open ) implementation of Smalltalk from the GNU project. There is even a Smalltalk Cross-compiler {Link without Title} for the PalmPilot which runs on Windows (though it seems that development may have stagnated in recent years).

There is also a high-performance JITted modular Smalltalk implementation designed for scripting called S# ( S-Sharp ) which supports an extended dialect of Smalltalk.

More recently, Python and Ruby have reimplemented many of Smalltalk's ideas with more mainstream syntax.


OBJECT-ORIENTED PROGRAMMING


As in other object-oriented languages, the central concept in Smalltalk is that of an ''object''. An object is always an ''instance'' of a ''class''. Classes are "blueprints" that describe the properties and behavior of their instances. For example, a Window class would declare that windows have properties such as the label, the position and whether the window is visible or not. The class would also declare that instances support operations such as opening, closing, moving and hiding. Each particular Window object would have its own values of those properties, and each of them would be able to perform operations defined by its class.

A Smalltalk object can do exactly three things:
  • Hold state (references to other objects).

  • Receive a message from itself or another object.

  • In the course of processing a message, send messages to itself or another object.


The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate.

Smalltalk is a 'pure' OO language, meaning that unlike Java and C++ there is no difference between objects and primitive types. In Smalltalk, primitive values such as integers, booleans and characters are also objects, in the sense that they are instances of corresponding classes. A programmer can change these classes to add new behavior to their instances--for example, to implement new control structures--or even change their current behavior. This fact is summarised in the commonly heard phrase "In Smalltalk everything is an object".

Since everything is an object, Classes themselves are also objects. Each class is an instance of the ''metaclass'' of that class. Metaclasses in turn are also objects, and are all instances of a class called 'Metaclass'. Code blocks (see below) are also objects.


REFLECTION

Smalltalk is a fully reflective system, implemented in itself. Smalltalk provides both structural and computational reflection. Smalltalk is a structurally reflective system whose structure is defined by Smalltalk objects. The classes and methods that define the system are themselves objects and fully part of the system that they help define. The Smalltalk compiler compiles textual source code into method objects, typically instances of CompiledMethod. The part of the class hierarchy that defines classes can add new classes to the system. The system is extended by running Smalltalk code that creates new classes and methods. In this way a Smalltalk system is a "living" system, carrying around the ability to extend itself at run-time.

Smalltalk also provides computational reflection, the ability to observe the computational state of the system. In implementations derived from the original Smalltalk-80 the current activation of a method is accessible as an object named via a keyword, thisContext. By sending messages to thisContext a method activation can ask questions like "who sent this message to me". These faclities make it possible to implement co-routines or Prolog-like back-tracking without modifying the virtual machine.

When an object is sent a message that it does not implement the virtual machine sends the object the doesNotUnderstand: message with a reification of the message as an argument. The message (another object, an instance of Message) contains the selector of the message and an Array of its arguments. In an interactive Smalltalk system the default implementation of doesNotUnderstand: is one that opens an error window reporting the error to the user. Through this and the reflective facilities the user can examine the context in which the error occurred, redefine the offending code, and continue, all within the system, using Smalltalk's reflective facilities.

But another important use of doesNotUnderstand: is intercession. One can create a class that does not define any methods other than doesNotUnderstand: and does not inherit from any other class. The instances of this class effectively understand no messages. So every time a message is sent to these instances they actually get sent doesNotUnderstand:, hence they intercede in the message sending process. Such objects are called proxies. By implementing doesNotUnderstand: appropriately one can create distributed systems where proxies forward messages across a network to other Smalltalk systems (a facility common in systems like CORBA and RMI but first pioneered in Smalltalk in the 1980s), persistent sysems where changes in state are written to a database and the like.


SYNTAX

Smalltalk syntax is rather minimalist, based on only a handful of declarations and reserved words. In fact, only five keywords are reserved in Smalltalk: true, '''false''', '''nil''', '''self''' and '''super'''. The only built-in language contructs are message sends, assignment, method return and literal syntax for some objects. The remainder of the language, including control structures for conditional evaluation and iteration, is implemented on top of those by the standard Smalltalk class library. (For performance reasons implementations may recognize and treat as special some of those messages; however, this is only an optimization, not hardwired into the language syntax).


Literals


The following examples illustrate the most common objects which can be written as literal values in Smalltalk methods.

Numbers. The following list illustrates some of the possibilities

42
-42
123.45
1.2345e2
2r10010010
16rA000

The last two entries are a binary and a hexadecimal number, respectively. The number before the 'r' is the Radix or base. The base does not have to be a power of two; for example 36rSMALLTALK is a valid number (for the curious, equal to 80738163270632).

Characters are written by preceding them with a dollar sign:



Strings are sequences of characters enclosed in single quotes:

'hello world'

To include a quote in a string, double it.

Two equal strings (strings are equal if they contain all the same characters) can be different objects residing in different places in memory. In addition to strings, Smalltalk has a class of character sequence objects called Symbol. Symbols are guaranteed to be unique--there can be no two equal symbols which are different objects. Because of that, symbols are very cheap to compare and are often used for language artifacts such as message selectors (see below).

Symbols are written as # followed by characters. For example:

#foo

Arrays:

#(1 2 3 4)

defines an array of four integers.

And last but not least, blocks

Some smalltalk code...

Blocks are explained in detail further in the text.

Many Smalltalk dialects implement additional syntaxes for other objects, but the ones above are the bread and butter supported by all.


Variable declarations


The two kinds of variable commonly used in Smalltalk are instance variables and temporary variables. Other variables and related terminology depend on the particular implementation. For example, VisualWorks has class shared variables and namespace shared variables, while Squeak and many other implementations have class variables, pool variables and global variables.

Temporary variable declarations in Smalltalk are variables declared inside a method (see below). They are declared at the top of the method as names separated by spaces and enclosed by vertical bars. For example:

  "" class="copylinks" target="_blank">:params <message-expressions>
  ":x" class="copylinks" target="_blank"> x + 1
  PositiveAmounts : allAmounts select: amt isPositive
  Vowels : aString select: aCharacter isVowel
  A String Object Responds To The "select:" Message By Iterating Through Its Members (by Sending Itself The Message "do:"), Evaluating The Selection Block ("aBlock") Once With Each Character It Contains As The Argument When Evaluated (by Being Sent The Message "value: Each"), The Selection Block (referenced By The Parameter "aBlock", And Defined By The Block Literal " ":aCharacter" class="copylinks" target="_blank"> aCharacter isVowel "), answers a boolean, which is then sent "ifTrue:" If the boolean is the object true, the character is added to a string to be returned
  Collisions : rectangles select: aRect containsPoint: aPoint