This page was last modified on 8 June 2016, at 14:59.
|Designed by||Alan Kay, Dan Ingalls, Adele Goldberg|
|Developer||Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, Scott Wallace, Peter Deutsch and Xerox PARC|
|Stable release||Smalltalk-80 version 2 / 1980|
|Typing discipline||strong, dynamic|
|Amber, Dolphin Smalltalk, GemStone/S, GNU Smalltalk, Pharo, Smalltalk/X, Squeak, VA Smalltalk, VisualWorks|
|Lisp, Simula, Euler, IMP, Planner, Logo,Sketchpad, ARPAnet, Burroughs B5000, cell (biology)|
|AppleScript, Common Lisp Object System, Dart, Dylan, Erlang, Etoys, Falcon, Go, Groovy, Io, Ioke, Java, Lasso, Lisaac, Logtalk, NewtonScript, Object REXX, Objective-C, PHP 5, Perl 6, Python, Ruby, Scala, Scratch, Self|
Smalltalk is an object-oriented programming language that was developed at Xerox PARC. As an integrated environment, it eliminates the distinction between programming language and operating system. It also allows its user interface and behavior to be customized. Smalltalk was the first object-oriented programming language to become popular. It was originally used to create prototypes of simpler programming languages and the graphical interfaces that are so popular today. Smalltalk was first run on Xerox's Alto computer, which was designed for it. In 1980, Smalltalk-80 was licensed to Tektronix, Apple, HP and TI for internal use. The first commercial release of Smalltalk was Methods from Digitalk in 1983, which later evolved into Visual Smalltalk. In 1997, Smalltalk became an ANSI standard (X3J20) .
- 1 Definitions, Nomenclature and Concepts
- 2 Smalltalk Language Syntax
- 2.1 Spaces and program layout
- 2.2 Comments
- 2.3 Literal Constants
- 2.4 Identifiers
- 2.5 Messages
- 2.6 Variables
- 2.7 Flow-of-Control Statements
- 3 References
Definitions, Nomenclature and Concepts
In Smalltalk, everything is about objects. Objects can be as simple as a number, a character or as complex as a graphic being displayed, windows, user dialogs or complete applications. In contrast to hybrid systems like C++ or Java, everything is an object in Smalltalk; this includes integers, characters, arrays, classes and even the programs stackframes, which hold the local variables during execution. In Smalltalk, there are no such things as "builtin" types or classes, which have to be treated different, or which do not behave exactly like other objects with respect to message sending, inheritance or debuggability.
Messages & Protocol
Objects communicate by sending messages to other objects. In other languages, this is sometimes called a "virtual function call", "virtual function" or even "function" alone. All of those other names are a bit misleading and, in the mathematical sense, actually wrong. We will therefore use messages for the act of asking for an operation by name, and, as we will see later, the term method for the actual code which will eventually perform the operation.
Classes & Instances
Since there are often many objects which share the same protocol, Smalltalk groups similar objects into classes. Every object is an instance of some class, and instances of the same class have the same protocol (although, each may have different internal state). This does not imply that instances of different classes must implement different protocols - actually, there are classes in the system (numeric classes, collection classes), which implement a common protocol, but are implemented completely different internally. This is called polymorphism.
Smalltalk is a class based object oriented language.
In Smalltalk every object is an instance of a class, and it is the class which describes the behavior of its instances (by defining the protocol and thereby defining how instances react to messages). Smalltalk allows access to this class at runtime and to gather information from that "class-thing". This is called reflection.
Because Smalltalk is a pure object oriented language, this "class thing" is also an object and therefore responds to a set of messages which is called its metaclass protocol (more on this below).
When a message is sent to an object, a corresponding action has to be performed - technically, a piece of code must be executed. This piece of code is called a method.
In Smalltalk (like in most other object oriented languages), classes are organized as a tree. Every class has a so called superclass, and is called a subclass of its superclass.
An object may contain internal state (also called attributes). In Smalltalk, this state usually consists of references to other objects (*). The slots which hold those references are called instance variables.
Since Smalltalk is a pure object oriented language, everything within the Smalltalk world is an object - this implies that every object's behavior is determined by its class. This is even true for classes themself - to the Smalltalk system, these are just like any other object and their protocol is specified by the classes class. These `class classes' are called Metaclasses.
Abstract classes are classes which are not meant to be instantiated (i.e. no instances of them are to be created). Their purpose is to provide common functionality for their subclass(es). In Smalltalk, the most obvious abstract class is the Object-Class, which provides a rich protocol useful for all kinds of objects (comparing, dependency mechanism, reflection etc.).
Smalltalk Language Syntax
Spaces and program layout
The Smalltalk syntax is format free. Spaces and line breaks may be added to a Smalltalk program without changing the meaning, except for the following:
- within an identifier (i.e. a variables name);
- within a numeric constant;
- within some compound tokens, such as the assignment token: ":=".
In Smalltalk a comment is anything enclosed in double-quotes ("). A comment may spawn multiple lines. Examples:
"some comment" "this is a multiline comment" " another multiline comment "
Literal constants in a Smalltalk source code are processed by the compiler, which creates corresponding objects at compilation time and places a reference to this constant into the generated code. The following literal constant types are allowed:
- Integer constants:
6, -1, 12345678901234567890
- Rational (Float) constants:
1.234, 1e10, 1.5e15
- FixedPoint constants (Scaled Decimals):
- Boolean constants:
- String constants:
'foo', 'a long string constant'
- Symbol constants:
#'bar', #'++', #'foo bar baz'
- Array constants:
#(1 #two #(3 4) #( #(5 6) 7) )
- ByteArray constants
#[0 1 2 3 4]
Identifiers (variable names) identify a variable. In Smalltalk, a variable holds a reference to some object.
By convention, uppercase identifiers are used for global- and class-Variables. Instance variables, arguments and local variables should start with a lowerCase character. You will really confuse other smalltalkers, if you do not follow this rule.
Special Identifiers (builtin names)
- nil - the one-and-only instance of the UndefinedObject class.
- true and false - the two boolean truth values.
- self - the receiver within a method.
- super - like self, but with different message lookup semantics if used as a message receiver.
- thisContext - the stackFrame object of the currently executing method or block as an object. Holds the receiver, message selector, arguments and local variables.
- here - like self, but with different message lookup semantics if used as a message receiver.
A message consists of three parts:
- the receiver;
- the message name, called the selector;
- optional arguments.
Messages without arguments are called Unary Messages. The name of a unary message (the "message selector") consists of a single word consisting of letters, digits or the underline character. The first character must not be a digit.
sends the message "negative" to the number 1, which is the receiver of the message.
This type of message allows for arguments to be passed with a message. A keyword message consists of one or more keywords, each followed by an argument. Each keyword is simply a name whereby the first character should be lower case by convention, and followed by a colon. The arguments may be literal constants, variables or other message expressions (must be grouped using parenthesis, if another keyword messages result is to be used as argument).
For instance, in the message:
5 between:3 and:8
"between:" and "and:" are the keywords, the numbers 3 and 8 are the arguments and the number 5 is the receiver of the message.
Unary messages have higher precedence than keyword messages.
A binary message takes 1 argument. Its selector is formed from one or two non-alphanumeric special characters. Some characters, such as braces, parenthesis or period cannot be used as binary selectors. Binary messages are typically used for arithmetic operations. However, this is not enforced by the system; no semantic meaning is known to the Smalltalk compiler, and binary messages could be defined and used for any class and any operation.
An example of a binary message is the one which implements arithmetic addition for numeric receivers (it is implemented in the Number classes):
1 + 5
This is interpreted as a message sent to the object 1 with the selector '+' and one argument, the object 5.
Binary messages parse left to right.
Message syntax summary
For some, Smalltalk's message syntax might seem strange at first. For example, consider the order to a person called "tom", to send an email message to a person called "jane" (assuming that tom, jane, theEmail refer to objects):
|tom, send an email to jane.||tom sendEmailTo: jane.||tom.sendEmail(jane); tom->sendEmail(jane);|
|tom, send theEmail to jane.||tom send: theEmail to: jane.||tom.sendEmail(theEmail, jane); tom->sendEmail(theEmail, jane);|
In Smalltalk, a variable holds a reference to some object - we say, a variable "is bound" to some object.
Global variables hold references to objects which are of common interest; especially, most classes can be referred to by a global variable. Beside classes, only a few other objects are bound to globals; the most interesting for now are:
- Transcript - refers to the transcript window, which can be used to send information messages to the user.
- Smalltalk - refers to the set of global variable bindings itself. It responds to messages to add, remove and query for global variables. Also, the command line arguments, language settings and some other configuration parameters are accessable via this object.
- Stdin, Stdout and Stderr - refer to the standard input, output and errorStreams. They are usually only relevant for non-graphical applications which deal with those input/output streams. When used with a GUI, Input/Output of Smalltalk is usually done through the Transcript or specialized Workspace windows.
These are variables with a visibility which is limited to a class and its subclasses or a limited group of classes.
A "Class Variable" is a single cell which is visible and shared by a class and all of its subclasses.
A "Class Instance Variable" is defined in a class as a slot of the class-objectm and is as such inherited by the subclasses. However, each of the class objects has its own private slot. This is very simular to the definition of an instance variable, which is also defined at one place and then inherited by subclasses. But with each individual instance having a possibly different value in it.
A "Shared Pool" is a collection of variable bindings which are visible to more than a single class. The pool itself defines the names of the variables it contains, and also provides the initialization code to set the initial values. Then other classes can "attach" to the pool, which makes these variables visible inside the class. Pool variables are readOnly for all places outside the SharedPool itself.
Instance variables are private to some object and their lifetime is the lifetime of the object. We will come back to instance variables, once we have learned how classes are defined.
A local variable declaration consists of an opening '|' (vertical bar) character, a list of identifiers and a closing '|'. It must be located before any statement within a code entity (a doIt-evaluation, block or method; the later being described below). For example:
| foo bar baz |
declares 3 local variables, named 'foo', 'bar' and 'baz'.
A local variables lifetime is limited to the time the enclosing context is active - typically, a method or a block.
Assigning a Value to a Variable
A variable is "bound to" (made to refer to) an object by an assignment expression.
Assuming, that "foo" and "bar" have beend declared as variables before, you can assign a value with:
foo := 1
bar := 'hello world'
Multiple assignments are allowed, as in:
foo := bar := baz := 1
Smalltalk does not have any keywords! Instead, each of the flow-of-control statements is done using a message.
The "If" Statement
(x < y) ifTrue: [ "in Smalltalk" max := y. i := j ] ifFalse: [ max := x. i := k ]
The "While" Statement
[i < 100] whileTrue: [ sum := sum + i. i := i + 1 ]
The "For" Statement
1 to: n do: [ :i | sum := sum + i. ]
The "Return" Statement
^ i+3 "in Smalltalk"