A Live Developer Journal

Pharo basic language examples using Smalltalk

Notes are based on Pharo by Example, a free book suggested by a Mooc called Live Object Programming in Pharo

Pharo basics tutorial commands

The following examples show you how to run commands, print commands and inspect commands, as well as what they do.

It looks like Smalltalk statements are finished with a fullstop.


There are three types of messages in Smalltalk: Unary, Binary and keyword. Unary messages are executed first, then binary messages and finally keyword messages: Unary > Binary > Keywords. Messages that contain equal precedence are executed left to right unless parentheses are used to change the order of evaluation.

Unary message examples

Unary messages are formatted like this: "anObject aMessage"

Binary message examples

Binary messages are formatted like this: "anObject + anotherObject"

Keyword message examples

Keyword messages are messages with arguments, they look like this: anObject akey: anotherObject akey2: anotherObject2

Cascade operator

The semi colon is the cascade operator. It is used to send multiple messages to the same receiver.

ProfStef next; next.

The above code in the tutorial goes to the next page and then to the next page by sending two next messages to the same ProfStef object. This part of the tutorial was confusing to me because a transcript example was used which made it difficult to figure out what was needed. I forgot that 'next' was the message and 'ProfStef' was the object so kept trying to write "ProfStef next; ProfStef next".


Blocks are anonymous methods that can be stored into variables and executed on demand. A block is contained within square brackets. Blocks are only executed when you send it a value message/s

Block with variable
	b := [:x | x+2].
	b value: 12.

This is what is happening:


Conditionals are just messages that are sent to Boolean objects.

Conditional example
  1 < 2
    ifTrue: [100]
    ifFalse: [42].

This is what is happening:


Loops are known as 'high level collection iterators' in the Smalltalk language. You can have to:do loops and to:by:do loops.

The following example is a to:do loop which returns a list of numbers from 1 to 100. Each number was converted to a string object in the process.

1 to: 100 do: [:i | Transcript show: i asString; cr].

The following example is a to:by:do loop. This loop starts with the number 100 and counts backwards by -2 until it gets to zero. The result is a list of even numbers from 100 to 0. Each number was converted to a string in the process.

100 to: 0 by: -2: [:i | Transcript show: i asString; cr].

The message 'do:' is sent to a collection of objects (collections can be an Array, Set, OrderedCollection). The do: message evaluates the block that follows for each element in the collection.

Here are a few more loop examples which replace the do: keyword messages with alternative keyword messages like ":collect", ":select" and ":reject".