GNU Smalltalk
Encyclopedia
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 binding
s for many free software libraries including SQLite
, libSDL, cairo
, gettext
, Expat
.
Some basic Smalltalk code:
:
Constructing and using a hash
:
:
Returning closures from a method:
Integer extend [
asClosure [
| value |
value := self.
^{ [ :x | value := x ]. [ value ] }
]
]
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 [
ifEven: evenBlock ifOdd: oddBlock [
^self even
ifTrue: [ evenBlock value: self ]
ifFalse: [ oddBlock value: self ]
]
]
Invoke the above method, passing it a block:
Iterating over enumerations and arrays using blocks:
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:
Using an enumeration and a block to square the numbers 1 to 10:
The above prints three names in reverse age order:
self halt
An optional message can be added to the exception; there's also
self halt: 'This is a message'
self error: 'This is a message'
These are actually wrappers for the actual exception raising method,
Exceptions are handled by
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:
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
by the GNU Project
GNU Project
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT. It initiated GNU operating system development in January, 1984...
.
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 binding
Language binding
In computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...
s for many free software libraries including SQLite
SQLite
SQLite is an ACID-compliant embedded relational database management system contained in a relatively small C programming library. The source code for SQLite is in the public domain and implements most of the SQL standard...
, libSDL, cairo
Cairo (graphics)
cairo is a software library used to provide a vector graphics-based, device-independent API for software developers. It is designed to provide primitives for 2-dimensional drawing across a number of different backends...
, gettext
Gettext
In computing, gettext is an internationalization and localization system commonly used for writing multilingual programs on Unix-like computer operating systems. The most commonly-used implementation of gettext is GNU gettext, released by the GNU Project in 1995.- History :gettext was originally...
, Expat
Expat (XML)
In computing, Expat is a stream-oriented XML 1.0 parser library, written in C. As one of the first available open-source XML parsers, Expat has found a place in many open-source projects. Such projects include the Apache HTTP Server, Mozilla, Perl, Python and PHP...
.
Examples
These examples only work on GNU Smalltalk 3.0 and later versions. Classic Hello world example:Some basic Smalltalk code:
Collections
Constructing and using an arrayArray data type
In computer science, an array type is a data type that is meant to describe a collection of elements , each selected by one or more indices that can be computed at run time by the program. Such a collection is usually called an array variable, array value, or simply array...
:
Constructing and using a hash
Hash table
In computer science, a hash table or hash map is a data structure that uses a hash function to map identifying values, known as keys , to their associated values . Thus, a hash table implements an associative array...
:
Blocks and iterators
Parameter-passing a block to be a closureClosure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
:
Returning closures from a method:
asClosure [
| value |
value := self.
^{ [ :x | value := x ]. [ value ] }
]
]
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:
ifEven: evenBlock ifOdd: oddBlock [
^self even
ifTrue: [ evenBlock value: self ]
ifFalse: [ oddBlock value: self ]
]
]
Invoke the above method, passing it a block:
Iterating over enumerations and arrays using blocks:
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:
Using an enumeration and a block to square the numbers 1 to 10:
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 (the default is to share the programmer-print and user-display representation) by overriding printOn:
.The above prints three names in reverse age order:
Exceptions
An exception is raised with ahalt
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
: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: