Previous | Table of Contents | Next |
by Allen Wirfs-Brock
This chapter is an introduction to programming in Smalltalk. It explains the basic concepts and constructs of the Smalltalk programming language and class libraries. The reader is assumed to be an experienced programmer who ideally has some exposure to object-oriented concepts and programming languages.
Early development of Smalltalk (Kay, 1996) took place at the Xerox Palo Alto Research Center during the 1970s. The development of Smalltalk at Xerox culminated with the release of Smalltalk-80 (Goldberg, 1993) in 1983. Since that time, a number of commercial and experimental implementations of Smalltalk have been created by companies and individuals. Most modern Smalltalk systems are either derived from or modeled after Smalltalk-80. However, substantial variation exists among Smalltalk implementations. X3J20 is a technical committee of the National Committee for Information Technology Standards that is chartered to produce an ANSI standard for the Smalltalk programming language. At the time of this writing (mid-1997), X3J20 is in the final stages of the development of this standard. The description of Smalltalk presented in this chapter is based on the most current X3J20 working drafts (X3J20, 1997) and should closely reflect the contents of the final standard. In this chapter, the phrase standard Smalltalk is used to specifically refer to Smalltalk as defined by the X3J20 committee.
Smalltalk is called an object-oriented programming language because a Smalltalk programmer is primarily concerned with defining and manipulating objects. A Smalltalk object is an entity within a program that has certain distinguishing characteristics:
All data within a Smalltalk program are represented as objects, and all computations are performed as manipulations of objects. Unlike many other object-oriented languages, Smalltalk does not distinguish between objects and more primitive non-object data types. Every datum, including integers and characters, within a Smalltalk program is uniformly treated as an object.
For a program to manipulate an object, it must be able to refer to that object. Within the text of a Smalltalk program, objects are referenced either as literals or as the values of variables. In Smalltalk, a literal is a syntactic form that defines an object. The most commonly used literals define objects that are numeric or string constants.
Decimal integer literals are sequences of decimal characters that define an object representing an integer numeric value. Nondecimal radices can be specified by preceding the digits of the literal with a radix specifier consisting of a decimal number followed by the letter r. A floating-point literal is a sequence of decimal digits with an embedded decimal point and an optional exponent specifier. Floating-point literals define objects that represent floating-point computational values. A decimal number can be optionally preceded by a hyphen to indicate a negative value. Some examples are listed here:
0 1024 -10000000000000 16rFFFF 8r177777 3.1416 1.0e10 1.0e-10
A character literal defines an object that represent an individual code point in a character set. A character literal is written by preceding a character with a dollar sign. A string literal defines an object that represents some fixed sequence of characters. A string literal is written as a sequence of characters enclosed within single quotes. For example, consider the following lines:
$x the character x $$ the character $ a Smalltalk string literal Alans language
Within the text of a Smalltalk program, a comment is represented by a sequence of characters enclosed in quotation marks. This chapter occasionally uses such comments in examples to clarify their meaning.
A reserved identifier is used as the literal representation of objects that represent the boolean truth values and for the unique object that is the initial value of all variables that are not explicitly initialized. The following represent reserved identifiers:
true false nil
An array literal defines an object that is a data structure aggregating other literal objects. An array literal consists of a hash symbol (#) followed by list of literals in parentheses. For example, consider the following lines:
#(1 -2 3 -4) an array of four small integers #(1 1.0 $1 one) a heterogeneous array #( #(1 true) #(0 false)) an array of arrays
Most objects do not have a literal syntax that can be used to reference them. Variables are used within the text of a Smalltalk program to reference arbitrary objects. Identifiers are used to name variables. Within Smalltalk identifiers, upper- and lowercase letters are considered to be distinct characters. By convention, variables with a limited scope of visibility are written with an initial lowercase letter, and variables with a relatively global scope are written with an initial uppercase letter. Standard Smalltalk permits the underscore character to be used within an identifier, but Smalltalks historic convention is to use embedded uppercase letters within an identifier to distinguish the individual words of a phrase. Some examples of Smalltalk variable names include the following:
temp1 a local variable Transcript a well-known global variable lastUpdateTime a variable with a descriptive name last_update_time a legal but non-idiomatic name
Previous | Table of Contents | Next |