GNU Smalltalk: Difference between revisions

→‎Examples: Changed wiki italics back to single quotes
m (Use the implementation template, removed link to "hello world")
(→‎Examples: Changed wiki italics back to single quotes)
 
(2 intermediate revisions by 2 users not shown)
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 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
Anonymous user