References



Smalltalk Homepage
Smalltalk Archive
Introduction to Smalltalk
History of Smalltalk









Smalltalk Compilers



Unix Compiler
Windows Compiler
Windows Compiler - 2

Smalltalk Programming Language


By: Resa D. Lockhart


Smalltalk is a simple and powerful programming language that was designed expressly to support the concepts of object-oriented programming. In the early 1970s, Alan Kay led a team of researchers at Xerox to invent a language that let programmers envision the data objects they intended to manipulate. Unlike C++, Smalltalk was not built on the syntax of a procedural language; it is a "pure" object-oriented language with more rigorously enforced rules than C++, which permits some of the procedural constructs of the C language. Smalltalk is said to be "pure" for one main reason: Everything in Smalltalk is an object, whereas in hybrid systems there are things which are not objects (for example, integers in C and Java).

I think that Smalltalk would be a good language to study further because it is said to be the first object-oriented language. Learning more about this language will help my classmates and myself to understand how current object-oriented languages evolved. I also think that Smalltalk is a very interesting language because of its unique block features and its class features. Because of its simplicity, and the fact that it is a "pure" object-oriented language, I also think that Smalltalk will be fun to learn.



Major Features of Smalltalk


Message Sending

All computations in Smalltalk are performed by objects sending messages to one another. A message is a name for a routine to execute, possibly together with some arguments to the routine. Smalltalk has a uniquely simple and elegant syntax for expressing message sending. There are three possible forms:

  1. receiver doSomething
  2. This is a "unary" message send. Write the receiver on the left and the message name (‘doSomething’, in this case) on the right. There are no arguments.

  3. receiver + argument
  4. This is a "binary" message send. As before, the receiver goes on the left, followed by the message name (‘+’), followed by exactly one argument. This form of message sending is always used for messages with "funny" numbers uses this syntax, and all such messages take exactly one argument.

  5. receiver take: 3 of: ‘candy’
  6. This is a "keyword" message send. Keywords ending in : (colon) alternate with arguments. The whole thing is considered a single message send. The message name is the concatenation of all the keywords (in this case, ‘take:of:’). There are as many arguments as there are keywords, and there can be as many keywords as you want.



Some Basic Functionality & Syntax

Smalltalk offers numbers, characters, strings, symbols, arrays, classes and objects as basic primitives. Comments may occur at any place in the code, as long as they are enclosed by double quotes. Numbers are written in the usual fashion, and there are integers, floating point numbers and fractions as subclasses. Characters must be prefixed by a dollar sign, strings are enclosed in single quotes, and elements of arrays are preceded by ‘#’ and enclosed in parentheses.

	7	"a number"
	$z	"a character"
	‘colourless ideas sleep furiously’	"a string"
	#(#tom   #dick    #harry)	"an array of 3 components"
	#(#one   ‘should shower at least’  3  ‘times a week’)	

The last expression shows that elements in array need not to me of the same type, since the array we use there contains 1 symbolic constant, 2 strings and a number.

Smalltalk identifiers may contain any sequence of letters and digits, but must start with a letter. No other symbols are allowed. Smalltalk draws a syntactic distinction between local and global variables, in which all global identifiers must start with an upper case letter, while local ones will always begin in lower case. Class names are assumed to be global and must therefore always start in upper case. Smalltalk is a "dynamically typed" language in which there is no type information association with identifiers, but rather with the objects themselves.

Local variables may be bound a instance variables attached to an object, or they may be explicitly declared as temporary variables. Such variable declarations are enclosed by vertical bars and separated by spaces.

|kermit skooter piggy henrietta|

declares four temporary variables.

If not otherwise requested all variables are initialized to null. The := symbol can be used to bind names to objects.

|fozzie|    fozzie := Bear new.

creates a new instance of class Bear and binds it to "fozzie", after which we may proceed to send instance messages to him until he expires.




Blocks

In Smalltalk, there are a number of selection statements and loops that may be specified.

  • ifTrue:
  • ifFalse:
  • ifFalse:ifTrue:
  • whileTrue:
  • whileFalse:
  • timesRepeat:
  • do:
  • to:do:
  • collect:

Smalltalk uses the notion of blocks to defer evaluation. One of the most useful features of Smalltalk is the ability to define "local functions" in the form of blocks. Blocks can be used to implement a wide variety of control structures. In fact, Smalltalk has no built-in control structures, but instead implements them in terms of blocks. Blocks are written as one or more Smalltalk statements between square brackets ([ and ]). Arguments to block functions can be specified by putting the argument names, prefixed by colons, before a vertical bars (|) at the beginning of the block.

	5 timesRepeat: [self print: ‘Hello World!’].

	primes := (1 to: 100) select: [:number | number isPrime].

	#(‘mary’ ‘had’ ‘a’ ‘little’ ‘lamb’)
		do: [:string | self print: string]
		separatedBy: [self space].

	[self shouldContinue]
		whileTrue: [self performAction].


Classes

A class defines which messages can be understood by members (also called instances) of that class, and it defines how objects respond to those messages. Since everything in Smalltalk is an object, everything has a class. For example, the number 5 has a class named Integer. The string ‘hello’ has a class named String, and so forth. You can define your own classes and create instances of them. You can find out what class any object belongs to by sending it the message ‘class’.

Classes themselves are instances of other classes called metaclasses. Metaclass programming is a relatively advanced part of Smalltalk programming, but it can be uses to achieve some important effects. The Smalltalk class browser makes both "ordinary class" and metaclass programming quite easy.

When you program in Smalltalk, you spend most of your time creating and modifying class definitions. In the Smalltalk class browser window, you can associate message names (also called selectors) with Smalltalk program code fragments (also called methods). A class, therefore, is a mapping between selectors and methods. When an instance of a class receives a message, it consults its class to see which method it should invoke in response to the message.