GNU Smalltalk: Difference between revisions

→‎Examples: Changed wiki italics back to single quotes
(→‎Examples: Changed wiki italics back to single quotes)
 
(3 intermediate revisions by 3 users not shown)
Line 1:
{{implementation|Smalltalk}}'''GNU Smalltalk''' is an implementation of the [[:Category:Smalltalk|Smalltalk]] programming language by the GNU Project. For download, see [http://smalltalk.gnu.org/ http://smalltalk.gnu.org/].
 
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.
 
==Examples==
These examples only work on GNU Smalltalk 3.0 and later versions. Classic [[Hello world]] example:
 
'Hello World!' displayNl
Line 10:
Some basic Smalltalk code:
 
<pre>"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'"</pre>
 
 
===Collections===
Line 92 ⟶ 93:
"=> 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 [[Haskell]]. For example:
 
#(1 3 5) inject: 10 into: [ :sum :element | sum + element ] "=> 19"
Line 115 ⟶ 116:
===Classes===
 
The following code defines a class named Person. By deriving from Magnitude, it automatically defines all comparison methods except one (<codett>&lt;</codett>). With the addition of that one, <codett>asSortedCollection</codett> 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 <codett>printOn:</codett>.
 
Magnitude subclass: Person [
Line 144 ⟶ 145:
 
===Exceptions===
An exception is raised with a <codett>halt</codett> call:
 
self halt
 
An optional message can be added to the exception; there's also <codett>error:</codett> 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, <codett>signal</tt>:
 
Error signal
Line 184 ⟶ 185:
* [http://www.gnu.org GNU]
* [http://smalltalk.gnu.org/ GNU Smalltalk]
 
[[Category:Smalltalk Implementations]]
Anonymous user