Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

Playground and Your First Smalltalk Programs

Write your first Smalltalk programs in the Pharo Playground: evaluate expressions, use temporary variables, print to the Transcript and inspect results, then build a small greeting and a counter.

The Playground is where Smalltalk programming starts. You type a few lines, run them, look at what they returned, and refine. There is no main, no file to save and no build step. In this lesson you will write several small programs and learn the habits that make Smalltalk development fast.

All Smalltalk code in this lesson is read-only (the site has no Smalltalk runner); JavaScript stand-ins let you play with the same logic.

Expressions always have a value

Every Smalltalk expression produces an object. When you "Print it", Pharo shows that object's printString.

SMALLTALK
2 + 3 * 4.        "-> 20, evaluated left to right"
'abc' , 'def'.    "-> 'abcdef' (comma concatenates)"
#(1 2 3) sum.     "-> 6"
7 isPrime.        "-> true"
1000000 * 1000000 * 1000000.  "-> 1000000000000000000 (no overflow)"

Notice 2 + 3 * 4 is 20, not 14: binary messages run strictly from left to right. We explain this in Unary, binary and keyword messages.

Smalltalk integers grow automatically to any size (SmallInteger, then LargePositiveInteger), so 100 factorial just works.

Temporary variables

In a Playground you can use a variable without declaring it, and it lives for the session of that window. In a method or a script you declare variables between vertical bars:

SMALLTALK
| name greeting |
name := 'Ada'.
greeting := 'Hello, ' , name , '!'.
greeting.

:= is assignment, . ends a statement, and the value of the last statement is the result you see with Print it. Variable names start with a lowercase letter and use camelCase.

Writing to the Transcript

The Transcript is the system log. Its messages mirror a text stream:

SMALLTALK
Transcript show: 'Hello, Smalltalk'; cr.
Transcript show: 'Total: ', 42 printString; cr.
Transcript show: 'a'; tab; show: 'b'; cr.

show: expects a string, which is why 42 printString converts the number first. cr ends the line and tab inserts a tab. The semicolons are cascades: they send several messages to the same receiver (Transcript). You will meet them properly in Cascades, assignment and statements. Open the Transcript from the World menu before running these lines.

JS Stand-in: Transcript with a cascade

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Inspecting instead of printing

Printing is great for numbers and short strings. For anything bigger, use Inspect it (Ctrl+I). Try it on:

SMALLTALK
Date today.
(1 to: 10) asOrderedCollection.
Smalltalk version.

The inspector shows each instance variable with its value. You can click into a value to inspect that too, and use the evaluator at the bottom, where self means the object you are looking at. This is called "exploring an object", and it is how Smalltalkers learn libraries: instead of reading documentation, they get an example object and look at it.

Program 1: a greeting for many names

SMALLTALK
#('Ada' 'Grace' 'Alan') do: [ :each |
    Transcript show: 'Hello, ', each; cr ].

Read it aloud: "to each element of this array, do this block". A block is code in square brackets, :each is its parameter, and | separates parameters from the body. Blocks are the subject of Blocks and closures.

Program 2: a counter that remembers

SMALLTALK
| count |
count := 0.
5 timesRepeat: [ count := count + 1 ].
count.          "-> 5"

timesRepeat: is a message sent to the number 5; its argument is a block. There is no for keyword anywhere.

JS Stand-in: greeting and counter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Fixing mistakes: the debugger as a helper

If you evaluate 3 + 'abc', Pharo opens the debugger with an error such as "Instance of ByteString did not understand #adaptToInteger:andSend:" (the exact text varies by version). Do not panic: this is the normal way Smalltalk reports trouble. You see the line that failed, the receiver and arguments, and you can change the code and continue. We cover this workflow in depth in the Tools chapter.

Good habits

  • Try, print, refine. Run small pieces, inspect results, then combine them.
  • Name things clearly. customerCount beats n.
  • Use printString / displayString. printString is a developer view ('abc' with quotes); displayString is the user view.
  • Read the library. Press Ctrl+B on any class name to see its methods.
  • Save the image before a long break.

Mini-exercise

Using the Playground, compute: the sum of the integers from 1 to 100, the length of the string 'Smalltalk', and whether 2024 is a leap year. Hint: look for messages like sum, size and isLeapYear (browse Year or Date classes). Smalltalk gives you the answers right where you type, so keep exploring.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does 2 + 3 * 4 return in Smalltalk?

    Binary messages are evaluated left to right with no special precedence for *, so (2 + 3) * 4 = 20.
  2. 2. What does the semicolon do in Transcript show: 'a'; cr?

    A cascade sends several messages to the receiver of the first message. A period ends a statement.
  3. 3. How do you declare temporary variables in a script?

    Temporaries go between vertical bars, separated by spaces.
  4. 4. Which tool is best for exploring the structure of an object?

    The inspector shows instance variables and lets you evaluate code with self bound to the object.

Summary

  • Every expression yields an object; Print it shows its printString.
  • Declare temporaries with | a b |, assign with :=, end statements with ..
  • The Transcript is the log; show:, cr and tab build output, usually in a cascade.
  • Inspect it explores objects instead of reading documentation.
  • Loops and repetition are messages with blocks, not keywords.

Next: Everything Is an Object