You are on page 1of 2

.....................................................................................................................................................................

.....................................................................................................................................................................

.....................................................................................................................................................................
Object subclass: #Counter
Pharo: a Live Programming Environment
instanceVariableNames: count initialValue
Pharo comes with an integrated development environment. classVariableNames:
Pharo is a live programming environment: you can modify your
package: MyCounter
objects and your code while your program is executing. All The method initialize is automatically invoked when a new
Pharo tools are implemented in Pharo: instance is created by sending the message new to the class i.e.,
Counter new.
a code browser with refactorings; Counter >> initialize An innovative, open-source Smalltalk-inspired
a debugger, a workspace, and inspectors; super initialize.
the compiler itself and much, much more. count := 0. language and system for live programming
Code can be inspected and evaluated directly in the image, Counter >> initialize is a notation to indicate that the
using simple key combinations and menus (open the contextual following text is the content of the method initialize in the http://www.pharo.org
menu on any selected text to see available options). class Counter.
Pharo is both an object-oriented, dynamically-typed general-
Methods purpose language and its own programming environment. The
The 5 Panes Pharo Code Browser language has a simple and expressive syntax which can be
Methods are public and virtual. They are always looked up in the
class of the receiver. By default a method returns self. Class learned in a few minutes. Concepts in Pharo are very consistent:
methods follow the same dynamic lookup as instance methods. Everything is an object: buttons, colors, arrays, numbers,
Methods Method factorial dened in class Integer. classes, methods. . . Everything!
Packages Classes Protocols
Integer >> factorial A small number of rules, no exceptions!
"Answer the factorial of the receiver."
self = 0 ifTrue: [^ 1]. Main Web Sites
self > 0 ifTrue: [^ self * (self - 1) factorial].
Code hosting http://smalltalkhub.com
Unit testing Questions https://pharoproject.slack.com/
Code pane
A test must be implemented in a method whose name starts Blog http://pharoweekly.wordpress.com
with test and in a class that inherits from TestCase. Contributors http://pharo.org/about
OrderedCollectionTest >> testAdd Topics http://topics.pharo.org
| added | Consortium http://consortium.pharo.org
The packages pane shows all the packages of the system. added := collection add: foo. Association http://association.pharo.org
The classes pane shows the class hierarchy of the selected self assert: (collection includes: foo).
package; the class side checkbox allows for getting the meth- The second line declares the variable added. The message PharoBooks
ods of the metaclass. assert: expects a true value. Pharo books are available at: http://books.pharo.org
The protocols pane groups the methods of the selected class
Pharo By Example, Deep into Pharo, Enterprise Pharo: a Web
to ease navigation. When a protocol name starts with a *, A simple, uniform and powerful model Perspective, Numerical Methods in Pharo, TinyBlog Tutorial,
methods of this protocol belong to a different package (e.g.,
Dynamic Web Development in Seaside (http://book.seaside.st)
the *Fuel protocol groups methods that belong to the Fuel Pharo has a simple dynamically-typed object model:
package); More books http://stephane.ducasse.free.fr/FreeBooks
everything is an object instance of a class;
The methods pane lists the methods of the selected protocol; classes are objects too and there is single inheritance be- Minimal Syntax
icons are clickable and trigger special actions; tween classes;
The source code pane shows the source code of the selected traits are groups of methods that can be reused orthogo- Six reserved words only
method. nally to inheritance; nil the undened object
Dening a class instance variables are protected; methods are public and true,false the boolean objects
virtual; and blocks are lexical closures. self the receiver of the current message
To add a class or edit a class, edit the proposed template!
Less is more: No type declarations, no primitive objects, no super the receiver but for accessing overridden
The following expression denes the class Counter as a sub-
generic types, no modiers, no operators, no inner classes, no methods
class of Object. It denes two instance variables count and
constructors, and no static methods. They are not needed! thisContext the current method or block activation
initialValue inside the package MyCounter.
.....................................................................................................................................................................

.....................................................................................................................................................................

.....................................................................................................................................................................
Minimal Syntax (II) 3+4 7 true, the block is executed and an exception is signaled.
Hello , World Hello World #(Hello World $!)
Object constructors & reserved syntactic constructs
do: [ :e | Transcript show: e ]
"comment" The + message is sent to the object 3 with 4 as argument. Send the message do: to an array. This executes the block
string collection of characters The string Hello receives the message , (comma) with once for each element, passing it via the e parameter. As a
#symbol unique string World as the argument. result, Hello World! is printed.
$a, Character space Two ways to create characters A keyword message can take one or more arguments that are
12 2r1100 16rC twelve (decimal, binary, hexa) inserted in the message name. Common Constructs
3.14 1.2e3 oating-point numbers
#(abc 123) literal array with the symbol Pharo allButFirst: 2. aro Conditionals
#abc and the number 123 [ :x | x + 2 ] value: 7 9
3 to: 10 by: 2. (3 to: 10 by: 2) condition if (condition)
{foo . 3 + 2} dynamic array built from 2 ex-
ifTrue: [ action ] { action(); }
pressions
The second line executes a block. The third example sends ifFalse: [ anotherAction ] else { anotherAction(); }
#[123 21 255] byte array
to:by: to 3, with arguments 10 and 2; this returns an interval
[ condition ] whileTrue: while (condition) { action();
| foo bar | declaration of two temporary containing 3, 5, 7, and 9.
[ action. anotherAction ] anotherAction(); }
variables
Message Precedence
var := expr assignment Loops/Iterators
exp1. exp2 period - expression separator Parentheses > unary > binary > keyword, and nally from left
1 to: 11 do: [ :i | for(int i=1; i<11; i++){
; semicolon - message cascade to right.
Transcript show: i ; cr ] System.out.println(i); }
[ :p | expr ] code block with a parameter
(10 between: 1 and: 2 + 4 * 3) not
<unary> method annotation | names | String [] names ={"A", "B", "C"};
<key:any wrd:#lit > with any literal arguments Messages + and * are sent rst, then between:and: is sent, names := #(A B C). for( String name : names ) {
^ expr caret - returns a result from a and then not. The rule suffers no exception: operators are names do: [ :each | System.out.print( name );
method just binary messages with no notion of mathematical precedence. Transcript show: each, , ] System.out.print(","); }
2 + 4 * 3 reads left-to-right and gives 18, not 14!
Collections start at 1.
aCol at:i accesses element at i and
Message Sending Cascade: Sending Muliple Messages to the Same Object
aCol at:i put:value sets element at i to value.
When we send a message to an object (the receiver), the cor- Using ; (a cascade) multiple messages are sent to the result of
Collections
responding method is selected and executed, and the method the same expression. Here ; arrives after add: 1, so messages
answers an object. Message syntax mimics natural languages, add: 2 and add: 3 are sent to add: 1s receiver: a collection. #(4 2 1) at: 3 1
with a subject, a verb, and complements. OrderedCollection new #(4 2 1) copy at: 3 put: 6 #(4 2 6)
add: 1; {4 . 2 . 1} at: 3 put: 6 #(4 2 6)
Pharo Java add: 2;
add: 3. (Array new: 2) add: 4; add: 2 ; yourself #(4 2)
aColor r: 0.2 g: 0.3 b: 0 aColor.setRGB(0.2,0.3,0) Set new add: 4; add: 4 ; yourself aSet
The whole message cascade value is the value of the last mes-
d at: 1 put: Chocolate. d.put("1", "Chocolate"); Dictionary new
sage sent (the symbol #g). To return the receiver of the message
cascade instead (i.e., the collection), send yourself as the last at: #a put: Alpha ; yourself aDictionary
Three Types of Messages: Unary, Binary, and Keyword
message of the cascade.
A unary message is one with no arguments. Files and Streams
Blocks
Array new. anArray work := FileSystem disk workingDirectory.
Blocks are objects containing code that is executed on demand.
#(4 2 1) size. 3 stream := (work / foo.txt) writeStream.
They are the basis for control structures: conditionals & loops.
stream nextPutAll: Hello World.
new is an unary message sent to classes (classes are objects). 2 = 2 stream close.
ifTrue: [ Error signal: Help ].
A binary message takes only one argument and is named by stream := (work / foo.txt) readStream.
Send the message ifTrue: to the boolean true (computed stream contents. Hello World
one or more symbol characters from +, -, *, =, <, >, ...
from 2 = 2) with a block as argument. Because the boolean is stream close.