GNU Smalltalk


GNU Smalltalk is an implementation of the Smalltalk programming language by the GNU Project.
The implementation, unlike other Smalltalk environments, uses text files for program input and interprets the contents as Smalltalk code. In this way, GNU Smalltalk acts more like an interpreter rather than an environment in the traditional Smalltalk manner.
GNU Smalltalk includes bindings for many free software libraries including SQLite, libSDL, cairo, gettext, and Expat.

Examples

These examples work only on GNU Smalltalk 3.0 and later versions. Classic Hello world example:

'Hello World!' displayNl

Some basic Smalltalk code:

"Everything, including a literal, is an object, so this works:"
-199 abs "199"
'gst is cool' size "11"
'Slick' indexOf: $c "4"
'Nice Day Isn''t It?' asLowercase asSet asSortedCollection asString "′?acdeinsty"

Collections

Constructing and using an array:

a := #
a at: 3 "3.14"
a reverse ""
a asSet "Set"

Constructing and using a hash:

hash := Dictionary from:.
hash at: 'fire' "Prints: hot"
hash keysAndValuesDo:
"Prints: water is wet
fire is hot"
hash removeKey: 'water' "Deletes 'water' -> 'wet'"

Blocks and iterators

Parameter-passing a block to be a closure:

"remember a block."
remember := .
"When the time is right -- call the closure!"
remember value: 'world'
"=> 'Hello, world!'"

Returning closures from a method:

Integer extend
]
blocks := 10 asClosure.
setter := blocks first.
getter := blocks second.
getter value "=> 10"
setter value: 21 "=> 21"
getter value "=> 21"

Using block to send info back to the caller:

Integer extend
ifFalse:
]
]

Invoke the above method, passing it a block:

10 ifEven: ifOdd: "=> 5"

Iterating over enumerations and arrays using blocks:

array := #
array do:
"=> 1"
"=> hi"
"=> 3.14"
do:
"=> 3"
"=> 4"
"=> 5"
"=> 6"

A method such as inject:into: can accept both a parameter and a block. It iterates over each member of a list, performing some function on while retaining an aggregate. This is analogous to the foldl function in functional programming languages. For example:

  1. inject: 10 into: "=> 19"

On the first pass, the block receives 10 as sum, and 1 as element, This returns 11. 11 then becomes sum on the next pass, which is added to 3 to get 14. 14 is then added to 5, to finally return 19.
Blocks work with many built-in methods:

withWriteStreamDo:
"File is automatically closed here"
linesDo:
"=> Wrote some text."

Using an enumeration and a block to square the numbers 1 to 10:

collect: "=> "

Classes

The following code defines a class named Person. By deriving from Magnitude, it automatically defines all comparison methods except one. With the addition of that one, asSortedCollection can sort by age. Note that we can override the way the object is printed/displayed by overriding printOn:.

Magnitude subclass: Person
< aPerson
name
name: value
age
age: value
printOn: aStream
group :=.
group asSortedCollection reverse

The above prints three names in reverse age order:

OrderedCollection Dan Cod

Exceptions

An exception is raised with a halt call:

self halt

An optional message can be added to the exception; there's also error: which raises a different kind of exception:

self halt: 'This is a message'
self error: 'This is a message'

These are actually wrappers for the actual exception raising method, signal:

Error signal
Error signal: 'Illegal arguments!'

Exceptions are handled by on:do: blocks.

on: Exception
do:

Of course you can catch only particular exceptions :

on: Warning
do:

It is possible to use the exception object, which is made available to the handler clause, to exit or resume the first block; exiting is the default, but can also be mentioned explicitly:

on: Error
do:
printNl "=> nil"
on: Warning do: "=> 5"