kids encyclopedia robot

Smalltalk facts for kids

Kids Encyclopedia Facts
Quick facts for kids
Smalltalk
Smalltalk Balloon.svg
Paradigm Object-oriented
Designed by Adele Goldberg, Dan Ingalls, Alan Kay
Developer Peter Deutsch, Adele Goldberg, Dan Ingalls, Ted Kaehler, Alan Kay, Diana Merry, Scott Wallace and Xerox PARC
First appeared 1972; 53 years ago (1972) (development begun 1969)
Stable release
Smalltalk-80 version 2 / 1980; 45 years ago (1980)
Typing discipline objects, but in some implementations, strong or dynamic
Scope Lexical (static)
Implementation language Smalltalk
Platform Xerox Alto (74181)
OS Cross-platform (multi-platform)
Major implementations
Amber, Dolphin Smalltalk, GemStone/S, GNU Smalltalk, Pharo, Smalltalk/X, Squeak, Cuis Smalltalk, Haver, VA Smalltalk, VisualWorks
Influenced by
Lisp, Simula, Euler, IMP, Planner, Logo, Sketchpad, ARPAnet, Burroughs B5000
Influenced
AppleScript, Common Lisp Object System, Dart, Dylan, Erlang, Etoys, Go, Groovy, Io, Ioke, Java, Lasso, Logtalk, Newspeak, NewtonScript, Object REXX, Objective-C, PHP 5, Python, Raku, Ruby, Scala, Scratch, Self, Swift

Smalltalk is a special kind of programming language that was created in the 1970s. It's known for being completely object-oriented. This means that everything in Smalltalk is treated as an "object."

Smalltalk was first made for education, especially to help kids learn by building things. Later, it became useful in business too. It was developed at Xerox PARC by a group of scientists, including Alan Kay, Dan Ingalls, and Adele Goldberg.

In Smalltalk, programs are built from small, self-contained units called objects. These objects are like mini-programs that know how to do things and store information. They talk to each other by sending messages. This all happens inside a special environment called a virtual machine.

Even though it's an older language, Smalltalk is still used today. When it first came out, Smalltalk-80 introduced many new ideas for object-oriented programming (OOP). It also offered an interactive programming environment, which means you could write and test code right away.

Smalltalk-like languages are still being developed. The official standard version, called ANSI Smalltalk, was approved in 1998. In 2017, Smalltalk was even voted one of the "most loved programming languages" in a big survey!

Smalltalk's History

There are many different versions of Smalltalk. When people say "Smalltalk," they often mean Smalltalk-80. This was the first version made public in 1980. The first computers to run Smalltalk were Xerox Alto machines.

Smalltalk was created by Alan Kay and his team at Xerox Palo Alto Research Center (PARC). Alan Kay designed most early versions. Adele Goldberg wrote much of the instructions, and Dan Ingalls built many of the first versions. The very first version, Smalltalk-71, was made quickly by Kay. It was based on the idea of message passing, like in another language called Simula.

Later versions, like Smalltalk-76, added more tools for developers. Smalltalk-80 then introduced metaclasses. This helped make sure that "everything is an object," even basic things like numbers.

Smalltalk-80 was the first version shared outside of PARC in 1981. Companies like Apple and HP got to try it out. A magazine called Byte even dedicated a whole issue to Smalltalk-80, helping its ideas spread widely. This version became the base for all future commercial Smalltalk versions.

Today, two popular versions of Smalltalk are Squeak and VisualWorks. Squeak is an open-source version that came from Smalltalk-80 Version 1. VisualWorks came from Smalltalk-80 Version 2.

In the 1980s and 1990s, Smalltalk was sold by companies like ParcPlace Systems and Digitalk. They tried to make Smalltalk popular, but it needed a lot of computer memory. Also, it was hard to connect it to common databases. Later, IBM also made its own Smalltalk version called VisualAge/Smalltalk.

Many companies struggled when Java became popular. However, companies like Cincom, GemTalk, and Instantiations still sell Smalltalk today. The open-source Squeak version has a very active community. It was even used to create the Etoys environment for the One Laptop per Child (OLPC) project. Pharo Smalltalk is another popular open-source version.

Since 2016, two web frameworks, Seaside and AIDA/Web, have become popular for building web applications in Smalltalk.

How Smalltalk Influenced Others

Smalltalk was one of the first object-oriented programming languages. It was inspired by Simula. Smalltalk has influenced almost all object-oriented languages that came after it. This includes languages like Java, Python, and Ruby.

Smalltalk was also very popular for new ways of developing software, like agile software development and rapid application development (RAD). Its fast and easy-to-use environment was perfect for quickly building and testing programs.

Smalltalk came from a bigger research effort funded by ARPA. This research helped create many things we use today, like hypertext (links on the internet), graphical user interfaces (GUIs), multimedia, the computer mouse, and the Internet itself. Alan Kay, one of Smalltalk's creators, also imagined a tablet computer called the Dynabook, which was similar to modern tablets like the iPad.

Smalltalk environments often created new ways of designing software. One famous example is the model–view–controller (MVC) pattern. This pattern helps organize how a program's data, display, and user actions work together. It's great for programs where you need to see the same information in different ways.

Smalltalk also played a big role in developing the graphical user interface (GUI) and "what you see is what you get" (WYSIWYG) interfaces. It helped create font editors and the idea of using a "desktop" on a computer screen. The powerful tools for finding errors and looking at objects in Smalltalk set a high standard for all future integrated development environments (IDEs).

Object-Oriented Programming in Smalltalk

Smalltalk80book
Smalltalk-80: The Language and its Implementation, also known as the "Blue book", an original book about the language

In Smalltalk-80, the main idea is the object. An object is always an example, or instance, of a class. Classes are like blueprints that describe what properties an object has and what it can do.

For example, a window on your computer screen might be an object. Its class would say that windows have properties like a title, a position, and whether they are visible. The class would also say that windows can do things like open, close, move, and hide. Each specific window object would have its own title and position, but they would all be able to open or close.

A Smalltalk object can do three main things:

  • It can hold information (references to other objects).
  • It can receive a message from itself or another object.
  • While processing a message, it can send messages to itself or other objects.

The information an object holds is private. Other objects can only ask for or change that information by sending messages. Any message can be sent to any object. The object then decides if it knows how to respond to that message. Alan Kay, one of the creators, said that sending messages is the most important idea in Smalltalk.

Unlike most other languages, you can change Smalltalk objects while the program is running. This makes it easy to fix things "on-the-fly" without restarting the whole system.

Smalltalk is a "pure" object-oriented language. This means that even basic things like numbers, true/false values, and characters are objects. You can even change how these basic objects work! This is why people say, "In Smalltalk, everything is an object."

Since classes are also objects, they can be asked questions. For example, a class can tell you what actions its objects can perform. This makes it easy to look at, copy, or save any object in the system.

Reflection: Looking at Itself

Reflection means a program can look at its own structure. It can see how it's built, like its parts or the types of information it uses. Smalltalk is a very reflective system. This means it can inspect and change its own code and data while it's running.

In Smalltalk-80, the classes and methods that define the system are also objects. This means they are part of the system they help create. You can add new classes and methods to the system while it's running. This makes a Smalltalk system a "living" system that can grow and change itself.

Because classes are objects, you can ask them questions like "What methods do you have?" or "What information do you store?". This makes it easy to examine and work with any object in the system using general code.

Smalltalk also lets you see what the program is doing right now. For example, you can ask "Who sent this message to me?". This helps create advanced features like how web frameworks handle the "back" button in a browser.

A great example of reflection in Smalltalk is how it handles errors. If an object receives a message it doesn't understand, it gets a special message called `doesNotUnderstand:`. In an interactive Smalltalk system, this usually opens an error window. From there, you can look at what happened, fix the code, and keep running the program without restarting!

You can even create special objects called "transparent proxies." These objects can intercept any message sent to them. This is useful for things like sending messages between different Smalltalk systems or getting objects from a database without you even knowing it.

Smalltalk's Simple Syntax

The way you write code in Smalltalk-80 is very simple. It uses only a few rules and special words. There are only six "keywords" that are reserved: `true`, `false`, `nil`, `self`, `super`, and `thisContext`. These are called pseudo-variables because you can't change what they mean.

The `true`, `false`, and `nil` pseudo-variables are unique single objects. `self` and `super` refer to the object that received a message. `thisContext` refers to the current part of the program that is running.

The main things you do in Smalltalk are sending messages, assigning values to variables, returning values from methods, and writing literal values (like numbers or text). Smalltalk's syntax is more like English than many other coding languages. Most of the language's features, like loops and if-then-else statements, are built using these basic message sends.

Here's a small example showing the basic parts of Smalltalk code:

exampleWithNumber: x
    | y |
    true & false not & (nil isNil) ifFalse: [self halt].
    y := self size + super size.
    #($a #a 'a' 1 1.0)
        do: [ :each |
            Transcript show: (each class name);
                       show: ' '].
    ^x < y

Literal Values

Here are some common types of values you can write directly in Smalltalk code:

Numbers:

42
-42
123.45
1.2345e2 "This is 123.45 in scientific notation"
2r10010010 "This is a binary number (base 2)"
16rA000 "This is a hexadecimal number (base 16)"

The number before the 'r' tells you the base. For example, `36rSMALLTALK` is a valid number!

Characters start with a dollar sign:

$A

Strings (text) are put in single quotes:

'Hello, world!'

To put a single quote inside a string, use two single quotes:

'I said, ''Hello, world!'' to them.'

Double quotes don't need special handling inside single-quoted strings:

'I said, "Hello, world!" to them.'

Smalltalk also has Symbols. These are like strings but are guaranteed to be unique. They are fast to compare and are often used for names of messages. Symbols start with `#`:

#'foo'

If the symbol doesn't have spaces or special characters, you can write it like this:

#foo

Arrays (lists of items):

#(1 2 3 4)

This creates an array with four numbers.

Code blocks (like mini-functions):

[... Some smalltalk code...]

These are explained more below.

Variables

Variables are names that hold values. In Smalltalk, you declare temporary variables inside a method. They are listed at the top, separated by spaces and enclosed in vertical bars. For example:

| index |

This declares a variable named `index`. It starts with a special value called `nil` (meaning "nothing"). You can declare multiple variables at once:

| index vowels |

Assigning Values

You give a value to a variable using `:=`.

vowels := 'aeiou'

This puts the string `'aeiou'` into the `vowels` variable.

Historically, Smalltalk used an underscore `_` or a left-arrow `←` for assignment. Some older code might still use `_`. Most modern Smalltalk versions accept both `_` and `:=`.

Messages

Sending a message is the most basic action in Smalltalk. Even things like "if-then-else" are done by sending messages.

Here's an example:

42 factorial

Here, `42` is the receiver of the message. `factorial` is the message selector. The `42` object will respond by calculating its factorial (42 * 41 * ... * 1). You can store the result in a variable:

aRatherBigNumber := 42 factorial

This is a unary message because it only involves one object (the receiver). Messages can also carry other objects as arguments:

2 raisedTo: 4

Here, `2` is the receiver, and `4` is the argument. The result will be 16. These are called keyword messages. Messages can have more arguments:

'hello world' indexOf: $o startingAt: 6

This message asks the string to find the character `$o` starting from position 6. The message selector is `indexOf:startingAt:`. The keywords (`indexOf:` and `startingAt:`) help explain what the arguments are for.

For example, to create a rectangle:

Rectangle width: 100 height: 200

`Rectangle` is the receiver (a class object). It receives the message `width:height:` with arguments `100` and `200`.

Smalltalk also uses special characters for binary messages, like math operations:

3 + 4

This sends the message `+` to the object `3` with `4` as an argument. The result is `7`.

3 > 4

This sends the message `>` to `3` with `4` as an argument. The result is `false`.

Smalltalk doesn't have built-in rules for what `+` or `>` mean. It's up to the object receiving the message (like a Number object) to decide how to respond. This allows for operator overloading, where different objects can understand the same symbols in their own way.

Expressions

You can combine many message sends in one expression. Smalltalk has a simple order for evaluating them: 1. Unary messages (like `factorial`) 2. Binary messages (like `+`, `-`, `*`, `/`) 3. Keyword messages (like `between:and:`)

Example:

3 factorial + 4 factorial between: 10 and: 100

This is calculated step-by-step:

  • `3 factorial` becomes `6`.
  • `4 factorial` becomes `24`.
  • `6 + 24` becomes `30`.
  • `30 between: 10 and: 100` becomes `true`.

The result of the last message sent is the result of the whole expression. You can use parentheses `()` to change the order of operations, just like in math.

(3 factorial + 4) factorial between: 10 and: 100

This would first calculate `3 factorial + 4` (which is `6 + 4 = 10`). Then `10 factorial` (which is `3,628,800`). Then `3,628,800 between: 10 and: 100` (which is `false`).

Because binary messages like `+` and `*` have the same importance, they are read from left to right. So, `3 + 4 * 5` is `(3 + 4) * 5`, which is `35`. If you want `3 + (4 * 5)` to get `23`, you need to use parentheses.

You can chain unary messages:

3 factorial factorial log

This sends `factorial` to `3` (result `6`), then `factorial` to `6` (result `720`), then `log` to `720`.

You can also send a series of messages to the same object using a cascade. You separate messages with semicolons `;`:

  Window new
    label: 'Hello';
    open

This first creates a new `Window` object. Then, it sends the `label:` message to that new window, and then the `open` message to the same new window. This is a neat way to send multiple messages without needing a temporary variable.

Code Blocks

A block of code is like a mini-program that you can write directly as a value. You use square brackets `[]` for this:

[ :parameters | <message-expressions> ]

The `:parameters` part is a list of inputs the block can take. For example:

[:x | x + 1]

This block takes an input `x` and returns `x + 1`. You can think of it like a math function `f(x) = x + 1`. To run a block, you send it the `value` message:

[:x | x + 1] value: 3

This would run the block with `3` as `x`, and the result would be `4`.

Blocks are very powerful. They can "remember" variables from where they were created. They are also first-class objects, meaning you can pass them around like any other value. Blocks make it easy to write clear and short code, especially for loops and other control structures.

How Smalltalk Controls Programs

Smalltalk doesn't have special keywords for control structures like `if` or `for`. Instead, these are handled by sending messages to objects.

For example, to do something only if a condition is true, you send the `ifTrue:` message to a Boolean object (`true` or `false`).

result := a > b
    ifTrue:[ 'greater' ]
    ifFalse:[ 'less or equal' ]

If `a > b` is `true`, the `true` object receives the `ifTrue:` message and runs the first block of code. If `a > b` is `false`, the `false` object receives the `ifFalse:` message and runs the second block.

Blocks are also used to create your own control structures or handle errors. For example, to select only the vowels from a string:

| aString vowels |
aString := 'This is a string'.
vowels := aString select: [:aCharacter | aCharacter isVowel].

Here, the string `'This is a string'` receives the `select:` message. The argument is a code block `[:aCharacter | aCharacter isVowel]`. This block checks if a character is a vowel. The `select:` method then goes through each character in the string, runs the block, and collects all characters for which the block returns `true`.

This `select:` method is defined in a general way so it can work with any collection of items, not just strings.

| rectangles aPoint collisions |
rectangles := OrderedCollection
  with: (Rectangle left: 0 right: 10 top: 100 bottom: 200)
  with: (Rectangle left: 10 right: 10 top: 110 bottom: 210).
aPoint := Point x: 20 y: 20.
collisions := rectangles select: [:aRect | aRect containsPoint: aPoint].

This code selects rectangles that contain a specific point.

Smalltalk also uses blocks for handling errors:

[
  some operation
] on:Error do:[:ex |
  handler-code
  ex return
]

If an error happens inside the first block, the code in the `do:` block runs. The `ex` variable gives you information about the error, and you can decide what to do next.

Classes: Blueprints for Objects

Here's how you define a new class in Smalltalk:

Object subclass: #MessagePublisher
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Smalltalk Examples'

This code sends a message to the `Object` class, telling it to create a new subclass named `MessagePublisher`. This shows that classes themselves are objects in Smalltalk! You can create them while the program is running.

Methods: What Objects Can Do

When an object receives a message, a method (a piece of code) that matches the message name is run. Here's a simple method definition:

publish
    Transcript show: 'Hello World!'

This defines what happens when a `MessagePublisher` object receives the `publish` message. It tells the `Transcript` (a window where you can see output) to show "Hello World!".

Here's a method that takes arguments and returns a value:

quadMultiply: i1 and: i2
    "This method multiplies the given numbers by each other and the result by 4."
    | mul |
    mul := i1 * i2.
    ^mul * 4

The method's name is `quadMultiply:and:`. The `^` symbol means "return this value."

Objects decide which method to run when they receive a message, and they do this while the program is running.

Creating Objects

To create a new object (an instance) of a class, you send the `new` message to the class:

MessagePublisher new

This creates and returns a new `MessagePublisher` object. You can store it in a variable:

publisher := MessagePublisher new

Or you can send messages to the new object right away:

MessagePublisher new publish

Hello World Example

The "Hello World" program is usually the first program you learn in a new language. In Smalltalk, it's very simple.

Transcript show: 'Hello, world!'.

This code sends the `show:` message to the `Transcript` object. The message's argument is the text `'Hello, world!'`. When this runs, the text will appear in the Transcript window (which is like a simple terminal). You need to have a Transcript window open to see the result.

Image-Based Saving

Most programming systems keep the program code separate from the program's running state (like objects and data). When you start a program, it loads the code, and any old data has to be loaded from files.

Smalltalk systems are different. They don't separate code and data. Classes are objects, just like everything else. So, most Smalltalk systems save the entire program's state (all objects, including classes) into one special image file. When you open Smalltalk again, it loads this image file, and your program is exactly as you left it. This is like taking a snapshot of your entire computer program.

This way of saving is great for quickly developing programs because all your work, including how the program is structured, is saved. This makes it easier to fix errors.

However, it also has some downsides. It can be harder to hide parts of the program from users, and it's not ideal for systems where many users need to access the same data at the same time.

Level of Access

In Smalltalk-80, almost everything can be changed while the program is running. This means you can change the tools you use to write code (the IDE) without restarting Smalltalk. In some versions, you can even change how the language works or how it manages memory while it's running! This makes Smalltalk very flexible and powerful.

Just-in-Time Compilation

Smalltalk programs are usually first turned into a special code called bytecode. This bytecode is then either run by a virtual machine or quickly translated into code that your computer's processor can understand. This process is called Just-in-time (JIT) compilation.

Smalltalk Versions and Implementations

OpenSmalltalk

OpenSmalltalk VM (OS VM) is a virtual machine that many modern Smalltalk versions are built on. It's written in a special subset of Smalltalk code. This code is then translated into C language code, which can run on many different computer systems. This allows Smalltalk programs to work on various platforms.

Notable Smalltalk versions based on OS VM include:

  • Squeak: The original open-source Smalltalk that OS VM was made for.
  • Pharo: An open-source Smalltalk for research and business.
  • Croquet VM: A Smalltalk VM related to Squeak, used for collaborative applications.

Other Implementations

  • Amber Smalltalk: Runs in web browsers using JavaScript.
  • Dolphin Smalltalk: From Object Arts.
  • Etoys: A visual programming system for learning.
  • GemStone/S: From GemTalk Systems.
  • GNU Smalltalk: A version without a graphical user interface.
  • ObjectStudio: From Cincom.
  • Scratch: A visual programming system (versions before 2.0 were Smalltalk-based).
  • Strongtalk: An open-source version for Windows that offers optional strong typing.
  • VisualAge Smalltalk: From IBM.
  • VisualWorks: From Cincom.

See also

Kids robot.svg In Spanish: Smalltalk para niños

kids search engine
Smalltalk Facts for Kids. Kiddle Encyclopedia.