forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmalltalk.html
More file actions
2092 lines (1995 loc) · 82.2 KB
/
Copy pathSmalltalk.html
File metadata and controls
2092 lines (1995 loc) · 82.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Smalltalk</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>Smalltalk</h2>
<h3><a name="Contents">Quick Links</a></h3>
<p>
<a href="#Accessors">Accessors</a>
<a href="#AddMethod">Add Method</a>
<a href="#Benchmarks">Benchmarks</a>
<a href="#BinaryMessages">Binary Messages</a>
<a href="#Blocks">Blocks</a>
<a href="#BrowseDoInspectPrintIt">Browse/Do/Inspect/Print It</a>
<a href="#BrowseEditMethod">Browse or Edit Method</a>
<a href="#Characters">Characters</a>
<a href="#Classes">Classes</a>
<a href="#CodeFormatting">Code Formatting</a>
<a href="#Comments">Comments</a>
<a href="#Configuration">Configuration</a>
<a href="#Constants">Constants</a>
<a href="#CreateClass">Create Class</a>
<a href="#CreateCategory">Create Category</a>
<a href="#CreateNewObject">Create New Object</a>
<a href="#Debugger">Debugger</a>
<a href="#DeleteClass">Delete Class</a>
<a href="#DeleteMethod">Delete Method</a>
<a href="#EnumeratedTypes">Enumerated Types</a>
<a href="#ExceptionHandling">Exception Handling</a>
<a href="#FFI">FFI</a>
<a href="#FileInOut">Filein/Fileout</a>
<a href="#FindClass">Find Class</a>
<a href="#FindList">File List</a>
<a href="#Flaps">Flaps</a>
<a href="#Fonts">Fonts</a>
<a href="#Images">Images</a>
<a href="#Implementers">Implementers</a>
<a href="#Implementations">Implementations</a>
<a href="#Indentation">Indentation</a>
<a href="#Inheritance">Inheritance</a>
<a href="#Inspector">Inspector</a>
<a href="#Interfaces">Interfaces</a>
<a href="#Introduction">Introduction</a>
<a href="#KeyboardInput">Keyboard Input</a>
<a href="#KeyboardShortcuts">Keyboard Shortcuts</a>
<a href="#LiteralDynamicArrays">Literal/Dynamic Arrays</a>
<a href="#LogicalExpressions">LogicalExpressions</a>
<a href="#MessagePrecedence">MessagePrecedence</a>
<a href="#Messages">Messages</a>
<a href="#MethodArguments">Method Arguments</a>
<a href="#MethodFinder">Method Finder</a>
<a href="#Methods">Methods</a>
<a href="#Morphic">Morphic</a>
<a href="#Monticello">Monticello</a>
<a href="#MouseButtons">Mouse Buttons</a>
<a href="#NamingConventions">Naming Conventions</a>
<a href="#OmniBrowser">OmniBrowser</a>
<a href="#OtherTypes">Other Types</a>
<a href="#Packages">Packages</a>
<a href="#Persistence">Persistence</a>
<a href="#Projects">Projects</a>
<a href="#ReasonsDisliked">Reasons Disliked</a>
<a href="#ReasonsLiked">Reasons Liked</a>
<a href="#Refactoring">Refactoring</a>
<a href="#Reflection">Reflection</a>
<a href="#RegularExpressions">Regular Expressions</a>
<a href="#ReservedKeywords">ReservedKeywords</a>
<a href="#Resources">Resources</a>
<a href="#RunningHeadless">Running Headless</a>
<a href="#WebApps">Seaside</a>
<a href="#Senders">Senders</a>
<a href="#SqueakEnvironment">Squeak Environment</a>
<a href="#StartingAndQuitting">Starting and Quitting</a>
<a href="#StartupAndShutdown">Startup and Shutdown</a>
<a href="#Statements">Statements</a>
<a href="#StreamIO">Stream I/O</a>
<a href="#Strings">Strings</a>
<a href="#SVI">SVI</a>
<a href="#Symbols">Symbols</a>
<a href="#SystemBrowshttp://vids.myspace.com/index.cfm?fuseaction=vids.individual&videoid=35394470er">System Browser</a>
<a href="#Transcript">Transcript</a>
<a href="#UnitTests">Unit Tests</a>
<a href="#VariableReferences">Variable References</a>
<a href="#Variables">Variables</a>
<a href="#WebApps">Web Applications</a>
<a href="#Windows">Windows</a>
<a href="#Workspace">Workspace</a>
<a href="#WorldMenu">World Menu</a>
</p>
<h3><a name="Introduction">Introduction</a></h3>
<table>
<tr>
<td width="215px">
"<a href="http://enfranchisedmind.com/blog/2008/10/16/the-hexagonal-wheel-paradox/#comment-33788">Any object-oriented language, given enough time,
will become a poor reinvention of Smalltalk.
</a>"<br />- Robert Fischer
</td>
<td rowspan="2">
<a href="http://wiki.squeak.org/squeak/3459">
<img src="../images/SmalltalkByteCover.jpg" height="160px"
alt="Smalltalk Byte cover"/>
</a>
</td>
</tr>
<tr>
<td>
"<a href="http://en.wikiquote.org/wiki/Alan_Kay">Actually
I made up the term "object-oriented" and
I can tell you I did not have C++ in mind.</a>"<br />- Alan Kay
</td>
</tr>
</table>
<p>
<i>The following was adapted from
<a href="http://www.squeak.org/Smalltalk/">http://www.squeak.org/Smalltalk/</a>.</i>
</p>
<ul>
<li>Everything is an object.</li>
<li>Classes are described in terms of state
(instance and class variables) and behavior (methods)
of the objects created from them.</li>
<li>Single inheritance is supported,
but multiple inheritance is not.</li>
<li>Objects communicate via message passing.</li>
<li>When an object receives a message, the corresponding method is
looked up in the class and superclasses of the receiver.</li>
<li>All methods are public.</li>
<li>All instance and class variables are private.</li>
<li>Uninitialized variables and container contents are nil.</li>
</ul>
<h3><a name="Resources">Resources</a></h3>
<ul>
<li><a href="http://www.smalltalk.org">Smalltalk.org</a></li>
<li><a href="http://www.whysmalltalk.com">Why Smalltalk?</a></li>
<li><a href="http://squeak.joyful.com/LanguageNotes">Language Notes</a></li>
<li><a href="http://smalltalk.gnu.org/">GNU Smalltalk</a>
- file-based, not image-based</li>
<li><a href="http://www.squeak.org">Squeak website</a></li>
<li><a href="http://wiki.squeak.org/squeak">Squeak wiki</a></li>
<li><a href="http://wiki.squeak.org/squeak/608">Squeak mailing lists</a></li>
<li><a href="http://people.squeakfoundation.org">Squeak People</a></li>
<li><a href="http://www.iam.unibe.ch/~ducasse/FreeBooks.html">
free online books</a></li>
<li>chat on irc.freenode.net using the #squeak channel</li>
<li><a href="http://www.bergel.eu/athena/">Athena</a></li>
<li><a href="http://www.info.ucl.ac.be/%7Ejbrichau/javaconnect.html">JavaConnect</a></li>
<li><a href="http://research.sun.com/projects/JSqueak/">JSqueak</a></li>
<li><a href="http://research.sun.com/projects/lively/">Lively Kernel</a></li>
<li><a href="http://sourceforge.net/projects/potatovm/">Potato</a></li>
<li><a href="http://MethodsAndMessages.vox.com">Randal Schwartz blog</a></li>
<li><a href="http://vst.ensm-douai.fr/noury/19">Smalltalk Pictures Repository</a></li>
<li><a href="http://stephane.ducasse.free.fr/Videos/SqueakOriginalMov/">Squeak training videos</a></li>
<li><a href="http://squeak.preeminent.org/tut2007/html/">Stephan Wessels Tutorial</a></li>
</ul>
<h3><a name="ReasonsLiked">Reasons some people like Smalltalk</a></h3>
<ol>
<li>It has a small, consistently applied syntax.</li>
<li>It has a great development environment consisting of tools such as
System Browser, Workspace, Transcript, Debugger,
Hierarchy Browser, Method Finder and more</li>
<li>Everything is an object.</li>
<li>It provides automatic version control.</li>
<li>It provides extreme polymorphism.
Any kind of object can be passed to a method
as long as it responds to the messages that will be sent to it.</li>
<li>It has a great
web app. framework (<a href="Seaside.html">Seaside</a>) and
a great CMS framework (<a href="Seaside.html#Pier">Pier</a>).</li>
</ol>
<h3><a name="ReasonsDisliked">Reasons some people dislike Smalltalk</a></h3>
<ol>
<li>It isn't as popular as many other programming languages.
<ul>
<li>Schools generally don't teach it.</li>
<li>Few jobs using it are available.</li>
<li>IT press doesn't talk about it.</li>
<li>It's difficult to convince others to use it.</li>
</ul>
</li>
<li>It doesn't minimize compile-time errors as much as
statically typed languages such as C++, C# and Java.
However, it does do incremental compiling when methods are saved
so it finds syntax errors before runtime,
unlike most scripting languages.</li>
<li>All the code for a project is stored in one big image file
(often over 30 MB).</li>
<li>The syntax is fairly different from most programming languages.
<ul>
<li>no dots or parentheses used in method calls</li>
<li>conditional and iterating constructs are method calls
instead of keywords</li>
<li>keyword messages are a departure from positional arguments</li>
<li>method cascading (sending multiple messages to the same object)
is a new concept</li>
</ul>
</li>
<li>Performance may be an issue.
However, VisualWorks Smalltalk is almost twice as fast as Python
which is faster than Perl which is faster than Ruby.</li>
<li>Classes are not in a namespace, so all class names must be unique.
Using class name prefixes is recommended.
This is important for using Squeak packages
and <a href="#Monticello">Monticello</a>.
Squeak has a <a href="http://wiki.squeak.org/squeak/3318">
prefix registry</a> in the wiki.
Note that Cincom Smalltalk does support namespaces.
</li>
</ol>
<h3><a name="Implementations">Implementations</a></h3>
<p>
This is a partial list of open source implementations.
</p>
<ul>
<li>F-Script - for Mac OS X Cocoa development</li>
<li>GNU Smalltalk</li>
<li>#Smalltalk (Sharp Smalltalk) - for .NET</li>
<li>Squeak</li>
<li>Talks2 - implemented in Java and can use Java libraries</li>
</ul>
<h3><a name="Concepts">Concepts</a></h3>
<h4><a name="Images">Images</a></h4>
<p>
The Smalltalk environment is stored in an "image" file.
It contains:
</p>
<ul>
<li>all the classes it shipped with</li>
<li>all the classes you've added and saved</li>
<li>modifications to supplied and added classes</li>
<li>appearance customizations</li>
<li>open windows and their locations</li>
</ul>
<p>
Changes can be saved in the current image or in a new image file.
</p>
<h4><a name="Classes">Classes</a></h4>
<p>
Classes are templates for creating objects.
They define class variables, instance variables,
class methods, instance methods, and more.
They are organized into categories (similar to packages in Java)
to make them easier to find,
but their names must be unique across all categories.
They are described by <code>Class</code> objects
that are in the Kernel-Class category.
In Squeak Smalltalk the <code>Class</code> class
is a subclass of <code>Behavior</code>.
</p>
<p>
The concepts of abstract classes and methods are not directly supported
in Smalltalk. A method is essentially abstract if
it returns "<code>self subclassResponsibility</code>".
A class is essentially abstract if it contains any abstract methods.
</p>
<p>
To get an <code>Array</code> containing all the current instances
of a given class, send <code>allInstances</code> to the class.
For example,
<code>instances := SystemWindow allInstances</code>.
</p>
<h4><a name="Inheritance">Inheritance</a></h4>
<p>
Smalltalk supports single inheritace, not multiple.
The superclass of every class is specified in its definition
and is often simply <code>Object</code>.
For example, <code>Object subclass: #FooBar</code>.
</p>
<p>
In Squeak Smalltalk, the <code>Object</code> class
is a subclass of <code>ProtoObject</code>.
</p>
<h4><a name="Interfaces">Interfaces</a></h4>
<p>
Smalltalk doesn't support the concept of interfaces.
Instead it relies on an extreme form of polymorphism.
Any message can be sent to any object as the object responds to it.
This makes creating mock objects really easy.
If the object doesn't respond to the message then
the method <code>doesNotUnderstand:</code> will be invoked
with the message name symbol as an argument.
This is defined in the class <code>Object</code> which
signals a <code>MessageNotUnderstood</code> error.
The <code>doesNotUnderstand</code> method can be overridden
to do interesting metaprogramming things.
</p>
<div class="code"><pre>
doesNotUnderstand: aMessage
"handle unrecognized messages"
Transcript show: 'I don''t recognize "', aMessage asString, '".'
</pre></div>
<h4><a name="Characters">Characters</a></h4>
<p>
Values representing a single character are specified with
a $ followed by the character. For example, the plus character
is represented by <code>$+</code>.
The <code>Character</code> class has class methods that return
whitespace characters such as <code>space</code> and <code>tab</code>.
</p>
<h4><a name="Strings">Strings</a></h4>
<p>
Literal strings are surrounded by single quotes.
This is convenient because it seems that double quotes
are needed inside string values more often than single quotes.
To embed a single quote, use two consecutive single quotes.
To concatenate strings, list any number of literal strings
and/or string variables with commas in between.
For example, <code>m := 'Mark'. f := m, 'Tami'.</code>
<code>f</code> is now <code>'MarkTami'</code>.
To concatenate non-string values to strings, call <code>asString</code>
on them. For example, "<code>n := 3. s := n printString, 'D'.</code>".
</p>
<p>
An alternative to converting non-<code>String</code> objects
to <code>Strings</code> for the purpose of concatenation
is to use the <code>String</code> <code>format:</code> method
whose parameter is a <code>Collection</code>.
The previous example could be written as follows.
</p>
<div class="code"><pre>
n := 3.
s := '3{1}' format: { n }
</pre></div>
<p>
The receiver <code>String</code> can contain any number of
indexed placeholders represented by an integer inside curly braces.
The parameter is often created using a dynamic array.
</p>
<p>
To append one String to another, a new String must be created.
For example, "<code>s1 := s1, s2</code>".
If many appends must be performed, consider using
a <code>WriteStream</code> as follows, which is much faster:
</p>
<div class="code"><pre>
s := String streamContents: [:stream |
stream
nextPutAll: 'foo';
nextPutAll: 'bar'
]
</pre></div>
<p>
To determine if a string contains a substring,
use the <code>includesSubstring:</code> method.
To replace every occurrence of a substring with another string,
use the <code>copyReplaceAll:with:</code> method.
</p>
<p>
To get a substring from a string, use the <code>copyFrom:to:</code>
method as follows:
</p>
<div class="code"><pre>
s1 := 'abcde'.
s2 := s1 copyFrom: 2 to: 4. "sets s2 to 'bcd'"
s3 := s1 copyFrom: 3 to: s1 size. "sets s3 to 'cde'"
</pre></div>
<p>
To get a substring that includes all characters but beginning ones
use the <code>allButFirst</code> and <code>allButFirst:</code> methods.
To get a substring that includes all characters but ending ones
use the <code>allButLast</code> and <code>allButLast:</code> method.
</p>
<h4><a name="Symbols">Symbols</a></h4>
<p>
Symbols are names that are preceded by a <code>#</code>.
They are used in place of strings when
there should only one instance with the name.
If a symbol name contains special characters such as
spaces, periods or underscores, surround the name with single quotes.
</p>
<h4><a name="RegularExpressions">Regular Expressions</a></h4>
<p>
Amazingly the standard Squeak distribution doesn't include
support for regular expressions. However, Squeak-Dev distributions
include the VB-Regex package. VB stands for Vassili Bykov, the author.
Among other things, this package adds methods to the <code>String</code>
class such as <code>matchesRegex:</code>.
See <a href="http://www.dartois-d.nom.fr/regex/en/">http://www.dartois-d.nom.fr/regex/en/</a> for documentation.
</p>
<div class="code"><pre>
'a1b' matchesRegex: '\w\d\w' "answers true"
</pre></div>
<h4><a name="OtherTypes">Other Types</a></h4>
<p>
Types that are primitives in other programming languages
are described by classes in Smalltalk.
For example, the "Kernel-Numbers" category contains the classes
<code>Float</code> (double-precision), <code>Fraction</code>
and <code>Integer</code> which are subclasses of <code>Number</code>.
The "Kernel-Objects" category contains the classes
<code>Boolean</code>, <code>False</code>, <code>True</code> and
<code>Object</code>.
</p>
<h4><a name="Constants">Constants</a></h4>
<p>
Constants are typically held in class variables.
They are initialized in a class initialize method.
This is automatically called when classes are loaded using
<a href="#Monticello">Monticello</a>.
What calls it if this isn't done?
</p>
<h4><a name="EnumeratedTypes">EnumeratedTypes</a></h4>
<p>
Smalltalk doesn't support the concept of enumerated types
like in Java 5 and above. Instead, the Smalltalk way is to:
</p>
<ol>
<li>create a class that represents the enumerated type</li>
<li>add a class variable for each enumerated value
(must start uppercase)</li>
<li>add a class-side <code>initialize</code> method that
creates an instance of the class for each enumerated value
using <code>basicNew</code>
and assigns it the corresponding class variable
<li>prevent creation of additional instances
by overiding the class method <code>new</code> with
"<code>self error: 'new instances cannot be created'</code>"</li>
<li>add class-side getter method for each enumerated value
that simply returns it</li>
</ol>
<p>
Here's an example <code>ColorEnum</code> class.
</p>
<div class="code"><pre>
Object subclass: #ColorEnum
instanceVariableNames: ''
classVariableNames: 'Blue Green Red'
poolDictionaries: ''
category: 'SomeCategory'
initialize
Red := self basicNew.
Green := self basicNew.
Blue := self basicNew
new
self error: 'new instances cannot be created'
red
^Red
green
^Green
blue
^Blue
</pre></div>
<h4><a name="Comments">Comments</a></h4>
<p>
Comments are surrounded by double quotes and can span multiple lines.
</p>
<p>
To add "todo" comments that can be easily found later,
enter "self flag: #todo." followed by a regular comment
which can be on the same line.
To find all occurrences of that, select "todo" and press alt-n
which displays all the senders of #todo in a new window.
In <a href="#OmniBrowser">OmniBrowser</a>, methods containing
those have a wrench icon in front of them in the method plane.
</p>
<h4><a name="LiteralDynamicArrays">Literal/Dynamic Arrays</a></h4>
<p>
These are Squeak extensions, not standard Smalltalk syntax.
</p>
<p>
Literal arrays create an array from a list of literal values.
The code <code>numbers := #(1 3 7)</code>
creates an array of Integer objects.
The code <code>colors := #(red green blue)</code>
creates an array of symbols.
The code <code>colors := #('red' 'green' 'blue')</code>
creates an array of String objects.
</p>
<p>
Dynamic arrays (a.k.a. bracket arrays) create an array from
expressions separated by periods.
The code <code>colors :=
{'red' asUppercase. 'green' asUppercase. 'blue' asUppercase}</code>
creates an array of String objects that are the results of
the <code>asUppercase</code> method.
</p>
<h4><a name="ReservedKeywords">Reserved Keywords</a></h4>
<p>
There are only six reserved keywords.
</p>
<ol>
<li><code>self</code> refers to the current object.</li>
<li>
<code>super</code> is used to force method lookup to
begin in the superclass.
</li>
<li>
<code>nil</code> represents no value and is a singleton instance
of the class <code>UndefinedObject</code>.
</li>
<li>
<code>true</code> and <code>false</code> are boolean values
that are singleton instances of the classes
<code>True</code> and <code>False</code>.
</li>
<li>
<code>thisContext</code> represents the top frame of the call stack
which represents the method or block that is currently executing.
</li>
</ol>
<h4><a name="LogicalExpressions">Logical Expressions</a></h4>
<p>
The <code>Boolean</code> class defines many methods for forming
logical expressions.
<code>and:</code> and <code>or:</code> short-circuit.
<code>&</code> and <code>|</code> do not.
Other methods include <code>not</code> and <code>xor:</code>.
</p>
<h4><a name="NamingConventions">Naming Conventions</a></h4>
<p>
Names of local/temporary variables and instance variables
start with a lowercase letter and are camelcased.
Names of classes, class variables and global variables
start with an uppercase letter and are camelcased.
</p>
<h4><a name="Indentation">Indentation</a></h4>
<p>
The convention in Squeak Smalltalk is to use a single tab.
</p>
<h4><a name="Statements">Statements</a></h4>
<p>
There are three kinds of statements in Smalltalk.
</p>
<ol>
<li>
<b>Message sends</b> send a message to a specified object.
They begin by specifying the "receiver" of the message.
To send a message to the current object, use the keyword
<code>self</code> or <code>super</code> for the receiver.
There are three kinds of messages: unary, binary and keyword.
For more on these,
see the <a href="#Messages">Messages</a> section below.
Messages can be "cascaded" to send multiple messages to the same object.
This is done by separating the messages with semicolons.
</li>
<li>
<b>Assignments</b> assign a value to a variable.
Their syntax is <code><i>variable</i> := <i>expression</i></code>
where <code><i>expression</i></code> is
a literal value, variable or message send.
</li>
<li>
<b>Answers</b> specify the return value of a method.
Their syntax is <code>^<i>expression</i></code>.
</li>
</ol>
<p>
There is also a syntax for defining methods,
but this is hidden by the development environment
and isn't usually viewed directly.
</p>
<p>
There are no statements for branching or looping.
Instead, those things are performed by methods in classes like
<code>Boolean</code>, <code>Interval</code> and <code>Collection</code>.
</p>
<p>
Here's an example if-then-else.
"<code>ifTrue:ifFalse:</code>" is a method of <code>Boolean</code>
which is what "<code>m > n</code>" returns.
The <code>show</code> method of <code>Transcript</code>
writes to the Transcript window if one is open.
</p>
<div class="code"><pre>
m := 4.
n := 5.
m > n
ifTrue: [
Transcript show: 'm is bigger'; cr
]
ifFalse: [
Transcript show: 'n is bigger'; cr
]
</pre></div>
<p>
The equivalent of a switch/case statement is the
<code>Object</code> method <code>caseOf:otherwise:</code>.
</p>
<p>
Here's an example loop through a range of numbers.
"<code>to:</code>" is an <code>Integer</code> method
that creates an <code>Interval</code> object.
"<code>do:</code>" is an <code>Interval</code> method
that executes a block for each value in it.
</p>
<div class="code"><pre>
3 to: 7 do: [ :i | Transcript show: i * 2; cr ]
</pre></div>
<p>Here's an example loop through an <code>Array</code> of symbols.</p>
<div class="code"><pre>
array := #(red green blue).
array do: [:item | Transcript show: item; cr]
</pre></div>
<h4><a name="Messages">Messages</a></h4>
<p>
There are three kinds of messages that can be sent to classes and objects.
</p>
<ol>
<li>
<b>Unary messages</b> take no arguments (ex. <code>5 factorial</code>).
</li>
<li>
<b>Binary messages</b> take one argument (ex. <code>2 + 3</code>).
Such methods should end with <code>^self</code> to support
chaining of calls.
These are mainly used for mathematical operators.
For other methods that take one argument, keyword messages are used.
For a complete list of binary messages, see
<a href="#BinaryMessages">Binary Messages</a>.
</li>
<li>
Keyword messages take any number of named arguments
(ex. <code>19 printPaddedWith: $+ to: 5.</code> which returns '+++19').
Argument names typically describe the kind of value they expect
(for example, <code>aBoolean</code>).
Despite being named, the arguments must be specified in the order
in which they are listed in the method definition.
When describing keyword messages, the arguments names are run together.
For example, the <code>Dictionary</code> class contains a method
described by <code>at:put:</code>.
</li>
</ol>
<p>
Any number of messages can be sent to the same object by chaining them
using semicolons. For example,
</p>
<div class="code"><pre>
s := Set new.
s add: 1; add: 2; add: 3.
</pre></div>
<h4><a name="BinaryMessages">Binary Messages</a></h4>
<p>
Characters that look like operators from other programming languages
are binary messages in Smalltalk. Many are defined in provided classes
and they can also be defined in custom classes.
There are a fixed set of them and they are all listed below.
</p>
<ul>
<li><code>+</code> <code>-</code> <code>*</code> <code>/</code>
perform arithmetic</li>
<li><code><</code> <code><=</code>
<code>></code> <code>>=</code> perform relational comparisons</li>
<li><code>=</code> tests for equality (same values)</li>
<li><code>~=</code> tests for inequality (different values)</li>
<li><code>==</code> tests for identity (same objects)</li>
<li><code>==</code> tests for non-identity (different objects)</li>
<li><code>**</code> performs exponentiation</li>
<li><code>//</code> performs integer division</li>
<li><code>\\</code> performs modulo</li>
<li><code>&</code> performs logical and (doesn't short-circuit)</li>
<li><code>|</code> performs logical or (doesn't short-circuit)</li>
<li><code>,</code> concatenate two collections (strings are collections)</li>
<li><code>@</code> creates a <code>Point</code> object</li>
<li><code>-></code> creates an <code>Association</code> object</li>
</ul>
<h4><a name="MessagePrecedence">Message Precedence</a></h4>
<p>
Smalltalk doesn't use operator precedence when evaluating statements.
Instead it evaluates statements in the following order
unless parentheses are used to override the order.
</p>
<ol>
<li>unary messages from left to right</li>
<li>binary messages from left to right</li>
<li>keyword messages from left to right</li>
<li>assignment</li>
</ol>
<p>
This means that <code>a := 2 + 3 * 4</code> will result in
<code>a</code> being set to 20,
not 14 as is the case in most other programming languages.
To get 14, use <code>a := 2 + (3 * 4)</code>.
</p>
<p>
This also methods that parentheses are needed in
"<code>v = 1 | v = 2</code>" because all the messages are binary
and will be evaluated left to right. The correct code is
"<code>(v = 1) | (v = 2)</code>".
</p>
<h4><a name="Blocks">Blocks</a></h4>
<p>
A block is a sequence of statements whose execution is deferred.
They can be thought of as methods with no name (anonymous)
Blocks can take parameters and return a value.
They are represented by instances of the <code>BlockClosure</code> class
which is in the Kernel-Contexts category.
</p>
<p>
The syntax for a block is
<code>[ argument-list | statement-list ]</code>.
This makes sense since real blocks have square corners.
Names in the argument list are preceded by colons
and they are separated by spaces.
For example, "<code>[ :p1 :p2 | <i>code</i> ]</code>".
If there are no arguments then the vertical bar can be omitted.
Before the statements, a list of temporary variables can be declared
in the same way they are declared
at the beginning of a <a href="#Methods">method</a>.
Statements in the statement list are separated by periods.
The Seaside coding conventions call for a single space
inside the square brackets.
</p>
<p>
Blocks can be passed as arguments to methods.
Once inside the a method, block arguments can be executed by sending
them many different methods from the <code>BlockClosure</code> class.
Examples include <code>value</code> (no args),
<code>value:</code> (1 arg),
<code>valueWithArguments:</code> (any number of args in an array),
<code>repeat</code>,
<code>doWhileFalse:</code> and <code>doWhileTrue:</code>.
</p>
<p>
Blocks are closures which means they remember the variables
that were in scope when they were created.
</p>
<p>
To run the code in a block in another process (or green thread),
see the methods starting with "<code>fork</code>"
in the <code>BlockClosure</code> class.
To "sleep" within a process, see the <code>Delay</code> class.
</p>
<h4><a name="Variables">Variables</a></h4>
<p>
There are five kinds of variables in Smalltalk.
</p>
<ol>
<li>
<b>Local variables</b> are only visible
in the scope of a method or block.
They are listed between vertical bars and separated by spaces
at the top of method definitions.
For example, <code>| var1 var2 var3 |</code>.
</li>
<li>
<b>Instance variables</b> are only visible
in instance methods of their class.
They can be referenced either with only their name
or their name preceded by <code>self</code>.
They are listed in a space-separated string that is the value of the
class definition <code>instanceVariableNames:</code> argument.
</li>
<li>
<b>Class variables</b> are only visible
in class and instance methods of their class.
They are listed in a space-separated string that is the value of the
class definition <code>classVariableNames:</code> argument.
</li>
<li>
<b>Global variables</b> are visible everywhere
and are maintained by the <code>Smalltalk</code> object
which is a global <code>SystemDictionary</code> object.
Most of the entries in the dictionary refer to
<code>Class</code> objects.
</li>
<li>
<b>Pool dictionaries</b> hold variables that are available to
all classes that list the dictionary name
in a space-separated string that is the value of the
class definition <code>poolDictionaries:</code> argument.
</li>
</ol>
<h4><a name="Methods">Methods</a></h4>
<p>
A method is an implementation of a message.
Methods are described by the message that invokes them
(called the method signature in other languages),
a comment, a list of local/temporary variables inside vertical bars
and a sequence of statements separated by periods.
The method signature can be just a method name (unary),
a method name followed by a single argument name (binary),
or list of keyword and argument name pairs
where each keyword is terminated with a colon (keyword).
The list of temporary variables is a space-separated list of
variable names surrounded by vertical bars.
The reason its good to have to declare variables in this way
is to make code typo-proof. If variables just sprang into existence
the first time they were used, typos in variables names would
create new variables rather than being flagged as errors.
The Seaside coding conventions call for a single space
inside the vertical bars.
</p>
<p>
It is a standard in Smalltalk to indent code using tabs instead of spaces.
</p>
<p>
It's not necessary to manually enter the local/temporary variables
between the vertical bars that declare them. When saving the method,
the compiler spots undeclared variables and offers the choice of
automatically declaring them as either instance or temporary variables.
It does this by displaying a dialog with "Unknown variable: {name}"
in the title bar and the choices "declare temp", "declare instance"
and "cancel".
</p>
<p>
A method can return a value using <code>^<i>expression</i></code>.
If a value is returned then by convention its comment
should begin with "answers ...".
Methods that don't explicitly return a value will return self.
When methods are cascaded, the value returned by the last method
is the value of the expression.
To make the return value of a cascaded sequence of messages
be the original receiver instead, send "<code>yourself</code>"
as the last message.
This is often useful in conjunction with creating a new object
that is returned from a method. For example,
</p>
<div class="code"><pre>
^Car new
make: 'BMW';
model: 'Z3';
paint: Color yellow;
yourself
</pre></div>
<p>
All methods are accessible to all other methods.
The fact that some methods are intended for use only by
other methods in the same class is documented by placing them
in a protocol named "private".
</p>
<h4><a name="Accessors">Accessors</a></h4>
<p>
Accessor methods for getting and setting the values of instance variables
do not, by convention, begin with "get" and "set".
For an instance variable "<code>foo</code>",
the getter method should be named "<code>foo</code>" and
the setter method should be named "<code>foo:</code>".
</p>
<p>
Accessor methods for all the instance variables can be generated
by yellow-clicking in the edit pane
and selecting refactor class...accessors.
Modify generated setters to have an argument name
that indicates the type of object expected.
</p>
<h4><a name="ExceptionHandling">Exception Handling</a></h4>
<p>
Exceptions are objects from classes that inherit from the
<code>Exception</code> class.
Browse the <code>Exception</code> class and
press the "hierarchy" button to see the predefined subclasses.
There are two direct subclasses,
<code>Abort</code> and <code>Error</code>.
All the other predefined exceptions inherit from <code>Error</code>.
</p>
<p>
To "signal" (throw in Java) an exception, create the exception object
and send the <code>signal</code> message to it.
</p>
<p>
To catch an exception, invoke code that might raise it in a block
and send a message for a <code>BlockClosure</code> method
such as <code>on:do:</code> to it.
</p>
<p>
Here's an example.
</p>
<div class="code"><pre>
thrower
"demonstrates throwing an exception"
| e |
e := Exception new.
e messageText: 'something bad happened'.
e signal.
catcher
"demonstrates catching an exception"
[ self thrower ] on: Exception do: [ :e |
Transcript show: e messageText
]
</pre></div>
<p>
The <code>BlockClosure</code> class has an <code>ensure:</code> method
that executes a given block regardless of
whether an exception is signaled (similar to a Java finally block).
It also has an <code>ifCurtailed:</code> method
that only executes the block if an exception is signaled.
</p>
<p>
Also look at these methods in the <code>Exception</code> class:
<code>pass</code>,
<code>resignalAs:</code>,
<code>resume</code>,
<code>retry</code>,
<code>retryUsing</code>,
<code>return</code>,
<code>return:</code>
</p>
<h4><a name="StreamIO">Stream I/O</a></h4>
<p>
I/O is performed using subclasses of the <code>Stream</code> class.
To read or write a text file, use a <code>FileStream</code>
which is a subclass of <code>ReadWriteStream</code>.
</p>
<p>
The following code writes to a new text file.
The <code>nextPutAll</code> method writes all elements of a collection
to the stream. Note that the <code>String</code> class
inherits from <code>AbstractCollection</code>.
Each character in the string is an element of the collection.
</p>
<div class="code"><pre>
fs := FileStream newFileNamed: 'foo.txt'.
fs nextPutAll: 'line 1'; cr.
fs nextPutAll: 'line 2'.
fs.close
</pre></div>
<p>The following code reads the text file.</p>
<div class="code"><pre>
fs := FileStream fileNamed: 'foo.txt'.
[fs atEnd] whileFalse: [
| line |
line := fs upTo: Character cr.
Transcript show: line.
].
fs.close
</pre></div>
<p>
To obtain the content of a stream as a String, send the
<code>contents</code> message to it.
</p>
<p>
The <code>close</code> method doesn't do anything.
Data can continue to be written to the stream after it is called.
</p>
<p>
To work with stdin and stdout of the current process,
load and use the OSProcess package.
The README file says that Mac OS X isn't supported yet.
To load the OSProcess package,
red-click the desktop and select the following:
open..., Universe Browser (basic), System, OSProcess,
"select package" button and "Install Selections" button.
Here's an example of using it:
</p>
<div class="code"><pre>
process := ThisOSProcess thisOSProcess.
stdin := process stdIn. "acts like a Stream"
stdout := process stdOut.
stderr := process stdErr.
stderr print: Time now; cr. "put the time of day on my stderr output"
</pre></div>
<h4><a name="Benchmarks">Benchmarks</a></h4>
<p>
To find out how long it takes to run a section of code, do the following:
</p>
<div class="code"><pre>
ms := [
<i>code goes here</i>
] timeToRun
</pre></div>
<h4><a name="Reflection">Reflection</a></h4>
<p>
To determine whether a variable refers to an object of a given type
use <code>isMemberOf</code> (instance of given class)
or <code>isKindOf</code> (instance of given class or a subclass).