GNU Smalltalk | |
Logo Size: | 220px |
Author: | Steve Byrne, Paolo Bonzini |
Latest Release Version: | 3.2.5 |
Operating System: | Unix (Linux, Cygwin, Mac OS X/Darwin) |
Genre: | Programming language |
License: | GPL + LGPL |
Website: | https://www.gnu.org/software/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.
These examples work only on GNU Smalltalk 3.0 and later versions. Classic Hello world example:
Some basic Smalltalk code:
Constructing and using an array:
a at: 3 "3.14"a reverse "((4 5) 2 1 3.14 'hi' 1)"a asSet "Set(1 'hi' 3.14 2 (4 5))"
Constructing and using a hash:
hash keysAndValuesDo: [:k :v | ('%1 is %2' % { k. v }) displayNl ]
"Prints: water is wet fire is hot"
hash removeKey: 'water' "Deletes 'water' -> 'wet'"
Parameter-passing a block to be a closure:
"When the time is right -- call the closure!"remember value: 'world'"=> 'Hello, world!'"
Returning closures from a method:
Using block to send info back to the caller:
Invoke the above method, passing it a block:
Iterating over enumerations and arrays using blocks:
(3 to: 6) do: [:item | item displayNl ]"=> 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:
On the first pass, the block receives 10 (the argument to inject) as sum, and 1 (the first element of the array) 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:
(File name: 'file.txt') linesDo: [:each | each displayNl ]
"=> Wrote some text."Using an enumeration and a block to square the numbers 1 to 10:
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 (the default is to share the programmer-print and user-display representation) by overriding printOn:
.
< aPerson [^self age < aPerson age ] name [^name ] name: value [name := value ] age [^age ] age: value [age := value ] printOn: aStream [aStream nextPutAll: ('%1 (%2)' % { name. age }) ]]
group := .
group asSortedCollection reverse
The above prints three names in reverse age order:
An exception is raised with a halt
call:error:
which raises a different kind of exception:signal
:
Exceptions are handled by on:do:
blocks.
Of course you can catch only particular exceptions (and their subclasses):
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:
(Warning signal: 'now what?') printNl "=> nil"[(Warning signal: 'now what?') printNl ] on: Warning do: [:ex | ex resume: 5 ] "=> 5"