-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpharoCheatSheet.tex
More file actions
486 lines (402 loc) · 17.7 KB
/
Copy pathpharoCheatSheet.tex
File metadata and controls
486 lines (402 loc) · 17.7 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
% By default, the contents of the back side of the final sheet is
% printed upside down. The option notumble suppresses that. Doing so
% may be necessary to suit the behavior of certain printing engines.
% Specifying [notumble] may also be useful during the writing of a
% document, to enable proof-reading on the screen.
\documentclass[notumble]{leaflet}
%\documentclass{leaflet}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage[default,scale=.82]{opensans}
% \renewcommand{\seriesdefault}{cl}
\renewcommand{\bfdefault}{sb}
% \renewcommand{\ttdefault}{fos}
\renewcommand\ttfamily{%
\fontfamily{\ttdefault}%
% \fontseries{lc}%
\fontshape{\shapedefault}%
\selectfont}
\usepackage{enumitem}
\setlist{
topsep=0pt,
partopsep=0pt,
parsep=1ex,
nolistsep,
itemsep=.1ex
}
\setlength{\parskip}{1ex}
% \newcommand{\myparagraph}[1]{\vspace{.6em}\textsc{#1}}
\newcommand{\myparagraph}[1]{#1---}
\usepackage[table,svgnames]{xcolor}
\usepackage{graphicx}
\usepackage{tabularx,booktabs}
\usepackage{alltt,textcomp}
\usepackage{url}
\urlstyle{sf}
\usepackage{ifthen}
\usepackage{xspace}
\newcommand{\eg}{\emph{e.g.,}\xspace}
\newcommand{\ie}{\emph{i.e.,}\xspace}
\newcommand{\etal}{\emph{et al.,}\xspace}
\newcommand{\ct}[1]{{\textsf{#1}}\xspace}
\usepackage{microtype}
\definecolor{light-gray}{rgb}{0.8,0.8,0.8}
\definecolor{comment}{rgb}{0.35,0.35,0.35}
\definecolor{string}{rgb}{0.5,0,0.5}
\definecolor{darkRed}{rgb}{0.5,0,0}
\definecolor{darkBlue}{rgb}{0,0,0.3}
\definecolor{darkCyan}{rgb}{0,0.3,0.3}
\makeatletter
\newsavebox{\code@box}
\newenvironment{displaycode}{%
\par
% \begin{lrbox}{\code@box}%
\hspace{1.5em}\begin{minipage}{\linewidth}
\begin{alltt}\small}{
\end{alltt}
\end{minipage}
% \end{lrbox}
% \usebox{\code@box}%
\par}
\makeatother
% Use of english prevents babel from adding spaces before $:
\newcommand{\code}[1]{\foreignlanguage{english}{\texttt{#1}}}
\graphicspath{{./figures/}}
\title{\vspace{-1.0cm}\normalfont
\includegraphics[width=0.8\linewidth]{logo.pdf}
\\[1.2\baselineskip]%
\vspace{-0.8cm}
\fontseries{cl}\selectfont\LARGE
An innovative, open-source Smalltalk-inspired \\ language and system for live programming\\
\url{http://www.pharo.org}}
\date{}
\setmargins{7.8mm}{7.8mm}{7.8mm}{7.8mm}
\CutLine*{1} \CutLine*{2} \CutLine*{3} \CutLine*{4} \CutLine*{5}\CutLine*{6}
\begin{document}
\maketitle
\thispagestyle{empty}
\vspace{-1.7cm}
Pharo is both an \emph{object-oriented}, \emph{dynamically-typed}
general-purpose language and its own programming environment. The
language has a simple and expressive syntax which can be learned
in a few minutes. Concepts in Pharo are \emph{very consistent}:
\begin{itemize}
\item Everything is an object: buttons, colors, arrays, numbers, classes, methods\ldots \emph{Everything!}
\item A small number of rules, no exceptions!
\end{itemize}
\begin{tiny}Pharo 14 - Sept 2025\end{tiny}
\vspace{-0.2cm}
\paragraph{Main Web Sites}
\begin{description}
\item[Core hosting] \url{http://github.com/pharo-project}
\item[Contributions] \url{http://github.com/pharo-contribution}
\item[New Tools] \url{http://github.com/pharo-spec}
\item[New Core Graphics] \url{http://github.com/pharo-graphics}
\item[Questions] \url{http://discord.gg/Sj2rhxn}
\item[Topics] \url{http://talks.pharo.org}
\item[Consortium] \url{http://consortium.pharo.org}
\item[Association] \url{http://association.pharo.org}
\end{description}
\vspace{-0.3cm}
\subsection{PharoBooks}
\textbf{Pharo books} are available at: \url{http://books.pharo.org}\\
Pharo with Style, Pharo By Example, Deep into Pharo, Enterprise Pharo: a Web Perspective, Application Building with Spec
TinyBlog Tutorial, Dynamic Web Development in Seaside
\textbf{More books} \url{http://stephaneducasse.github.io/freebooks}
%\pagebreak{}
\vspace{-0.3cm}
\subsection{Minimal Syntax}
\noindent
\begin{tabularx}{\linewidth}{@{}rX@{}}
\toprule
\multicolumn{2}{c}{Six reserved words only}\\
\midrule
\textcolor{darkRed}{\code{nil}} & the undefined object\\
\textcolor{darkRed}{\code{true}},\textcolor{darkRed}{\code{false}} & the boolean objects\\
\textcolor{darkCyan}{\code{self}} &the receiver of the current message\\
\textcolor{darkCyan}{\code{super}} &the receiver but for accessing overridden methods\\
\textcolor{darkCyan}{\code{thisContext}} &the current method or block activation\\
\end{tabularx}
\subsection{Minimal Syntax (II)}
\noindent
\begin{tabularx}{\linewidth}{@{}rX@{}}
\toprule
\multicolumn{2}{c}{Object constructors \& reserved syntactic constructs}\\
\midrule
\textcolor{comment}{\code{"}{comment}\code{"}}& \\
\textcolor{string}{'string'}&sequence of characters\\
\textcolor{string}{\code{\#symbol}}&unique string\\
\textcolor{darkRed}{\code{\$a}}, Character space&two ways to create characters\\
\textcolor{darkRed}{\code{12 5r22 16rC}}&twelve (decimal, 5-base, hexa)\\
\textcolor{darkRed}{\code{3.14 1.2e3}}&floating-point numbers\\
\code{\#(\textcolor{string}{abc} \textcolor{darkRed}{123})}&compiled time literal array\\
\code{\{\textcolor{darkBlue}{foo}\,.\ \textcolor{darkRed}{3}\,+\,\textcolor{darkRed}{2}\}}&runtime array whose elements are evaluated at runtime\\
\code{\#[\textcolor{darkRed}{123 21 255}]} &byte array \\
\midrule
\code{|\,\textcolor{darkBlue}{foo bar}\,|} &declaration of two temporary variables \\
\code{var := \emph{expr}}&assignment \\
\code{\emph{exp1}. \emph{exp2}} &period - statement separator\\
\code{;} &semicolon - message cascade\\
\code{[\,:\textcolor{darkBlue}{p}\,|\,}\emph{expr}\code{\,]} &code block with a parameter \\
\code{<unary>}&method annotation \\
\code{<key:\emph{'any'}wrd:\emph{\#lit}>}&with any literal arguments\\
\code{\textasciicircum\ \emph{expr}} &caret - return/answer a result from a method\\
\bottomrule
\end{tabularx}
%\clearpage
%\vspace{-0.3cm}
\subsection{Message Sending}
When we send a message to an object (the \emph{receiver}), the corresponding method is selected and executed, and the method answers an object. Message syntax mimics natural languages, with a subject, a verb, and complements.
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\textbf{Pharo} & \textbf{Java}\\
%\multicolumn{2}{c}{\textbf{Conditionals}}\\
\midrule
aColor r: 0.2 g: 0.3 b: 0&aColor.setRGB(0.2,0.3,0)\\
d at: '1' put: 'Chocolate'.&d.put("1", "Chocolate");\\
\midrule
\end{tabularx}
\vspace{-0.3cm}
\subsection{Three Types of Messages: Unary, Binary, and Keyword}
Computation happens by sending messages to objects.
A \textbf{unary message} has no arguments.
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\textcolor{darkBlue}{Array} new.& $\rightsquigarrow$ anArray \\
\#(\textcolor{darkRed}{4 2 1}) size.& $\rightsquigarrow$ 3\\
\midrule
\end{tabularx}
\code{new} is an unary message sent to classes (classes are objects).
A \textbf{binary message} takes only one
argument and is named by one or more symbol characters from \code{+}, \code{-}, \code{*}, \code{=}, \code{<}, \code{>}, ...
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\textcolor{darkRed}{3} + \textcolor{darkRed}{4}& $\rightsquigarrow$ 7 \\
\textcolor{string}{'Hello'} , \textcolor{string}{' World'}& $\rightsquigarrow$ 'Hello World'\\
\midrule
\end{tabularx}
The \code{+} message is sent to the object
\textcolor{darkRed}{\code{3}} with \textcolor{darkRed}{\code{4}} as
argument. The string \textcolor{string}{\code{'Hello'}} receives the message \code{,}
(comma) with \textcolor{string}{\code{'~World'}} as the argument.
A \textbf{keyword message} can take one or more
arguments that are inserted in the message name.
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\textcolor{string}{'Pharo'} allButFirst: \textcolor{darkRed}{2}.& $\rightsquigarrow$ 'aro' \\
\ [ :x | x + 2 ] value: 7 & $\rightsquigarrow$ 9 \\
\textcolor{darkRed}{3} to: \textcolor{darkRed}{10} by: \textcolor{darkRed}{2}.& $\rightsquigarrow$ (3 to: 10 by: 2)\\
\midrule
\end{tabularx}
The second line executes a block (lexical closure).
The third example sends
\code{to:by:} to \textcolor{darkRed}{\code{3}}, with arguments
\textcolor{darkRed}{\code{10}} and \textcolor{darkRed}{\code{2}}; this
returns an interval containing \textcolor{darkRed}{\code{3}},
\textcolor{darkRed}{\code{5}}, \textcolor{darkRed}{\code{7}}, and
\textcolor{darkRed}{\code{9}}.
\vspace{-0.3cm}
\subsection{Message Precedence}
Parentheses have the highest precedence, followed by unary, binary, and keyword messages — all evaluated left to right.
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
(\textcolor{darkRed}{15} between: \textcolor{darkRed}{1} and: \textcolor{darkRed}{2}\,+\,\textcolor{darkRed}{4}\,*\,\textcolor{darkRed}{3}) not& $\rightsquigarrow$ false \\
\midrule
\end{tabularx}
Messages \code{+} and \code{*} are sent first, then \code{between:and:} is sent, and \code{not}.
The rule suffers no exception: operators are just binary messages with \emph{no notion of mathematical precedence}.
Mathematical operators like + and * are not special; they are ordinary binary messages. This means 2 + 4 * 3 is evaluated strictly left to right, giving 18, not 14.
\code{\textcolor{darkRed}{2}\,+\,\textcolor{darkRed}{4}\,*\,\textcolor{darkRed}{3}} reads left-to-right and gives \textcolor{darkRed}{18}, not \textcolor{darkRed}{14}!
%\clearpage
\vspace{-0.3cm}
\subsection{Cascade: Sending Muliple Messages to the Same Object}
Using \code{;} (a cascade) multiple messages are sent to the result of the same expression.
Here \code{;} arrives after \ct{add: 1}, so messages \ct{add: 2} and \ct{add: 3} are sent to \ct{add: 1}'s receiver: a collection.
\begin{displaycode}
\textcolor{darkBlue}{OrderedCollection} new
add: \textcolor{string}{1};
add: \textcolor{string}{2};
add: \textcolor{string}{3}.
\end{displaycode}
The whole message cascade value is the value of the last message sent (here 3). To return the receiver of the message cascade instead (\ie the collection), send
\code{yourself} as the last message of the cascade.
\vspace{-0.3cm}
\subsection{Blocks}
Blocks are objects containing code that is executed on demand. They are the basis for control structures:
conditionals \& loops.
\begin{displaycode}
\textcolor{darkRed}{2} = \textcolor{darkRed}{2}
ifTrue: [ \textcolor{darkBlue}{Error} signal: \textcolor{string}{'Help'} ].
\end{displaycode}
Send the message \code{ifTrue:} to the boolean
\textcolor{darkRed}{\code{true}} (computed from
\code{\textcolor{darkRed}{2} = \textcolor{darkRed}{2}}) with a block
as argument. Because the boolean is \textcolor{darkRed}{\code{true}},
the block is executed and an exception is signaled.
\begin{displaycode}
\#(\textcolor{string}{'Hello World'} \textcolor{darkRed}{\$!})
do: [ :\textcolor{darkBlue}{e} | \textcolor{darkBlue}{e} traceCr ]
\end{displaycode}
Send the message \code{do:} to an array. This executes the block
once for each element, passing it via the \code{e} parameter. As a
result, \code{\textcolor{string}{Hello~World!}} is printed.
\vspace{-0.3cm}
\section{Common Constructs}
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\multicolumn{2}{c}{\textbf{Conditionals}}\\
\midrule
condition&if (condition) \\
\ \ \ \ \ ifTrue: [ action ] & \ \ \ \{ action(); \} \\
\ \ \ \ \ ifFalse: [ anotherAction ]& \ \ \ else \{ anotherAction(); \}\\
\midrule
\ \ [ condition ] whileTrue:&while (condition) \{ action();\\
\ \ \ \ \ \ \ [ action. anotherAction ]&\ \ \ anotherAction(); \}\\
\end{tabularx}
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\multicolumn{2}{c}{\textbf{Loops/Iterators}}\\
\midrule
1 to: 11 do: [ :i | &for(int i=1; i<=11; i++)\{ \\
\ \ \ \ i traceCr ]&\ \ \ \ System.out.println(i); \}\\
\midrule
| names | &String [] names =\{"A", "B", "C"\};\\
names := \#('A' 'B' 'C').&for( String name : names ) \{\\
names do: [ :each |&\ \ System.out.print( name );\\
\ \ \ (each, ' , ') traceCr]&\ \ System.out.print(","); \} \\
\midrule
\end{tabularx}
Collections start at 1. \code{aCol~at:i} accesses element at \ct{i} and \code{aCol at:i put:value} sets element at \code{i} to \code{value}.
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\multicolumn{2}{c}{\textbf{Collections}}\\
\midrule
\#(4 2 1) at: 3& $\rightsquigarrow$ 1 \\
\#(4 2 1) copy at: 3 put: 6; yourself& $\rightsquigarrow$ \#(4 2 6) \\
\{4 . 2 . 1\} at: 3 put: 6 ; yoursel& $\rightsquigarrow$ \#(4 2 6) \\
\midrule
OrderedCollection new add: 4; \\ add: 2 ; asArray& $\rightsquigarrow$ \#(4 2) \\
Set new add: 4; add: 4 ; yourself&$\rightsquigarrow$ aSet(4) \\
Dictionary new& \\
\ \ \ at: \#a put: 'Alpha' ; yourself&$\rightsquigarrow$a~Dictionary(\#a->'Alpha')\\
\midrule
\end{tabularx}
\noindent
\begin{tabularx}{\linewidth}{@{}lX@{}}
\toprule
\multicolumn{2}{c}{\textbf{Files and Streams}}\\
\midrule
work := FileSystem disk workingDirectory. \\
stream := (work / 'foo.txt') writeStream. \\
stream nextPutAll: 'Hello World'. \\
stream close. \\
stream := (work / 'foo.txt') readStream. \\
stream contents. & $\rightsquigarrow$ 'Hello World' \\
stream close.
\end{tabularx}
%\clearpage
\section{Pharo: a Live Programming Environment}
Pharo comes with an integrated development environment. Pharo
is a \emph{live programming environment}: you can modify your objects
and your code while your program is executing. All Pharo tools are
implemented in Pharo:
\begin{itemize}
\item a code browser with refactorings;
\item a debugger, a workspace, and inspectors;
\item the compiler itself and much, much more.
\end{itemize}
Code can be inspected and evaluated directly in the image, using
simple key combinations and menus (open the contextual menu on any
selected text to see available options).
% \begin{center}
% \includegraphics[width={.6}\textwidth]{workspace}
% \end{center}
\subsection{The 5 Panes Pharo Code Browser}
\begin{center}
\includegraphics[width=0.9\textwidth]{nautilus-anotated}
\end{center}
\begin{itemize}
\item The \emph{packages} pane shows all the packages of the system.
\item The \emph{classes} pane shows the class hierarchy of the
selected package; the \emph{class side} checkbox allows for getting
the methods of the metaclass.
\item The \emph{protocols} pane groups the methods of the selected
class to ease navigation. When a protocol name starts with a \emph{*}, methods of this
protocol belong to a different package (\eg the \emph{*Fuel}
protocol groups methods that belong to the \emph{Fuel} package);
\item The \emph{methods} pane lists the methods of the selected
protocol; icons are clickable and trigger special actions;
\item The \emph{source code} pane shows the source code of the
selected method.
\end{itemize}
\vspace{-0.3cm}
\subsection{Defining a class}
To add a class or edit a class, edit the proposed template!
The following expression defines the class \code{Counter} as a subclass of \code{Object}.
It defines two instance variables \code{count} and \code{initialValue} inside the package \code{MyCounter}.
\begin{displaycode}
Object << #Counter
slots: \{ #count . #initialValue \};
package: 'MyCounter'
\end{displaycode}
The method \code{initialize} is automatically invoked when a new instance is created by sending the message \code{new} to the class i.e., \code{Counter new}.
\begin{displaycode}
\textit{Counter >>} initialize
super initialize.
count := 0.
\end{displaycode}
\code{\textcolor{darkBlue}{\textit{Counter}\,>{}>\,}initialize} is a \textit{notation} to indicate that the following text is the content of the method \code{\textbf{initialize}} in the class \code{\textcolor{darkBlue}{Counter}}.
\vspace{-0.3cm}
\subsection{Methods}
Methods are public and virtual. They are always looked up in the class of the receiver. By default a method returns \code{self}.
Class methods follow the same dynamic lookup as instance methods.
Method \code{factorial} defined in class \ct{Integer}.
\begin{displaycode}
\textit{Integer >>} factorial
"Answer the factorial of the receiver."
self = 0 ifTrue: [^ 1].
self > 0 ifTrue: [^ self * (self - 1) factorial].
\end{displaycode}
%\pagebreak{}
%\vspace*{\fill}
\vspace{-0.3cm}
\subsection{Unit testing}
A test must be implemented in a method whose name starts with \code{test} and in a class that
inherits from \code{TestCase}.
\begin{displaycode}
\textcolor{darkBlue}{\textit{OrderedCollectionTest}\,>>}\,testAdd
| \textcolor{darkBlue}{added} |
\textcolor{darkBlue}{added} := \textcolor{darkBlue}{collection} add: \textcolor{string}{'foo'}.
\textcolor{darkCyan}{self} assert: (\textcolor{darkBlue}{collection} includes: \textcolor{string}{'foo'}).
\end{displaycode}
The second line declares the variable \code{\textcolor{darkBlue}{added}}. The message \code{assert:} expects a true value.
\vspace{-0.3cm}
\section{A simple, uniform and powerful model}
Pharo has a simple dynamically-typed object model:
\begin{itemize}
\item everything is an object --- instance of a class;
\item classes are objects too and there is single inheritance between classes;
\item traits are groups of methods that can be reused orthogonally to inheritance;
\item instance variables are protected; methods are public and virtual;
and blocks are lexical closures.
\end{itemize}
\textbf{Less is more:} No type declarations, no primitive
objects, no generic types, no modifiers, no operators, no inner
classes, no constructors, and no static methods. They are not needed!
%\vfill{}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: