I'm using Pharo. It's a bit buggier than Squeak, but it has a nicer interface.
- No files; everything is stored within the development environment repository. The legacy of this still seeing in the eclipse development environment, which came from IBM's Smalltalk IDE.
- The class browser is like a file system browser. Note the similarity to Mac OS X's finder browser. It's amazing how influential the early days at Xerox Parc continue to be. Using the browser, you can find classes and methods and generally poke around.
- Adding classes or methods is a matter of clicking in the right spot in the browser. Class definitions are done with message sent as is just about everything else. Note that our Smalltalk implementation we use a slightly different syntax for class definitions. Instance variables are strings sent via the subclass: method.
- There is no main method. We use workspace to "do it", "print it", or "inspect it".
- The transcript is like a log.
Transcript show: 'hi'..
Literals. From ``Squeak by Example,'' by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet with Damien Cassou and Marcus Denker. Printed page 52 on Smalltalk syntax.
keywords. self, super, nil, true, false (yes, that's it).
x := 1.
[:x | n := n + x.]. These are lexical closures in that they capture all variables visible to them and can access them even if the code block is passed to another function. This is in contrast to just about every other language you've probably looked at. But, it's essentially required to implement Smalltalk. see the discussion below on control structures.
list at: i put: 'hi'.
- precedence. left to right but unary messages have highest precedence then binary then keyword.
- return values from methods.
^expr.or the final expression value in a code block.
Create parrt category at the top level of the browser. Then create a sample
Test class and put a method inside to count the characters within a string:
Execute by saying following the workspace window.
Highlight it and say "print it". It will emit
3 to the workspace window.
By convention, Smalltalk class objects receive
new messages to create new instances. Also by convention, the basic implementation of
Object will send message
initialize to the newly created object. Here is what the
new class method does in my definition of Object:
Notice that initialize is an instance method the others are class methods.
Blocks and Control structures
Blocks are chunks of code with parameters and return values like anonymous functions. Unlike functions, however, they can see the variables in the surrounding context. In the example from the previous section, notice that the block can access local variable
To evaluate a block, we use message
value:value:, etc... For example,
boolean-value ifTrue: [what to do if true]
boolean-value ifTrue: [what to do if true] ifFalse: [do if not true]
From "Squeak by example" page 59:
[condition] whileTrue: [loop code]
From "Squeak by example" page 59:
There is also
Most collections implement
do: so that we can iterate across their elements. From "Squeak by example" page 59-60: "The most important messages for iterating over collections include do:, collect:, select:, reject:, detect: and inject:into:." and then:
"collect: builds a new collection of the same size, transforming each element.
select: and reject: build new collections, each containing a subset of the elements satisfying (or not) the boolean block condition. detect: returns the first element satisfying the condition. Don’t forget that strings are also collections, so you can iterate over all the characters.
Finally, you should be aware that collections also support a functional- style fold operator in the inject:into: method. This lets you generate a cumula- tive result using an expression that starts with a seed value and injects each element of the collection. Sums and products are typical examples.
This is equivalent to 0+1+2+3+4+5+6+7+8+9+10."
Sample linked list
Using the syntax we'll use for class projects: