Pharo

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

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

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

.....................................................................................................................................................................
Files and Streams • The methods pane lists the methods of the selected protocol;
icons are clickable and trigger special actions;
work := FileSystem disk workingDirectory.
• The source code pane shows the source code of the selected
stream := (work / ’foo.txt’) writeStream.
method.
stream nextPutAll: ’Hello World’.
stream close. Defining a class
stream := (work / ’foo.txt’) readStream.
To add a class or edit a class, edit the proposed template!
stream contents. ’Hello World’
The following expression defines the class Counter as a sub-
An innovative, open-source Smalltalk-inspired
stream close.
class of Object. It defines two instance variables count and language and system for live programming
initialValue inside the package MyCounter.
Pharo: a Live Programming Environment Object subclass: #Counter http://www.pharo.org
instanceVariableNames: ’count initialValue’
Pharo comes with an integrated development environment. classVariableNames: ’’ Pharo is both an object-oriented, dynamically-typed general-
Pharo is a live programming environment: you can modify your package: ’MyCounter’ purpose language and its own programming environment. The
objects and your code while your program is executing. All The method initialize is automatically invoked when a new language has a simple and expressive syntax which can be
Pharo tools are implemented in Pharo: instance is created by sending the message new to the class i.e., learned in a few minutes. Concepts in Pharo are very consistent:

• a code browser with refactorings; Counter new. • Everything is an object: buttons, colors, arrays, numbers,
Counter >> initialize classes, methods. . . Everything!
• a debugger, a workspace, and inspectors;
super initialize.
• the compiler itself and much, much more. • A small number of rules, no exceptions!
count := 0.
Code can be inspected and evaluated directly in the image, Counter >> initialize is a notation to indicate that the Main Web Sites
using simple key combinations and menus (open the contextual following text is the content of the method initialize in the
menu on any selected text to see available options). class Counter. Code hosting http://smalltalkhub.com
Questions http://discord.gg/Sj2rhxn
Methods Blog http://pharoweekly.wordpress.com
The 5 Panes Pharo Code Browser
Methods are public and virtual. They are always looked up in the Contributors http://pharo.org/about
class of the receiver. By default a method returns self. Class Topics http://topics.pharo.org
methods follow the same dynamic lookup as instance methods. Consortium http://consortium.pharo.org
Methods Method factorial defined in class Integer. Association http://association.pharo.org
Packages Classes Protocols
Integer >> factorial
PharoBooks
"Answer the factorial of the receiver."
self = 0 ifTrue: [^ 1]. Pharo books are available at: http://books.pharo.org
self > 0 ifTrue: [^ self * (self - 1) factorial]. Pharo By Example, Deep into Pharo, Enterprise Pharo: a Web
Unit testing Perspective, Numerical Methods in Pharo, TinyBlog Tutorial,
Code pane
Dynamic Web Development in Seaside (http://book.seaside.st)
A test must be implemented in a method whose name starts
with test and in a class that inherits from TestCase. More books http://stephane.ducasse.free.fr/FreeBooks
OrderedCollectionTest >> testAdd
Minimal Syntax
• The packages pane shows all the packages of the system. | added |
• The classes pane shows the class hierarchy of the selected added := collection add: ’foo’.
Six reserved words only
self assert: (collection includes: ’foo’).
package; the class side checkbox allows for getting the meth-
The second line declares the variable added. The message nil the undefined object
ods of the metaclass.
assert: expects a true value. true,false the boolean objects
• The protocols pane groups the methods of the selected class
self the receiver of the current message
to ease navigation. When a protocol name starts with a *,
A simple, uniform and powerful model super the receiver but for accessing overridden
methods of this protocol belong to a different package (e.g.,
methods
the *Fuel protocol groups methods that belong to the Fuel
Pharo has a simple dynamically-typed object model: thisContext the current method or block activation
package);
.....................................................................................................................................................................

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

.....................................................................................................................................................................
Minimal Syntax (II) 3+4 7 from 2 = 2) with a block as argument. Because the boolean is
’Hello’ , ’ World’ ’Hello World’ true, the block is executed and an exception is signaled.
Object constructors & reserved syntactic constructs
#(’Hello World’ $!)
"comment" The + message is sent to the object 3 with 4 as argument. do: [ :e | Transcript show: e ]
’string’ sequence of characters The string ’Hello’ receives the message , (comma) with Send the message do: to an array. This executes the block
#symbol unique string ’ World’ as the argument. once for each element, passing it via the e parameter. As a
$a, Character space two ways to create characters A keyword message can take one or more arguments that are result, Hello World! is printed.
12 2r1100 16rC twelve (decimal, binary, hexa) inserted in the message name.
3.14 1.2e3 floating-point numbers Common Constructs
#(abc 123) literal array with the symbol ’Pharo’ allButFirst: 2. ’aro’
#abc and the number 123 [ :x | x + 2 ] value: 7 9 Conditionals
{foo . 3 + 2} dynamic array built from 2 ex- 3 to: 10 by: 2. (3 to: 10 by: 2)
condition if (condition)
pressions
The second line executes a block. The third example sends ifTrue: [ action ] { action(); }
#[123 21 255] byte array
to:by: to 3, with arguments 10 and 2; this returns an interval ifFalse: [ anotherAction ] else { anotherAction(); }
| foo bar | declaration of two temporary containing 3, 5, 7, and 9.
[ condition ] whileTrue: while (condition) { action();
variables
Message Precedence [ action. anotherAction ] anotherAction(); }
var := expr assignment
exp1. exp2 period - statement separator Parentheses > unary > binary > keyword, and finally from left Loops/Iterators
; semicolon - message cascade to right.
1 to: 11 do: [ :i | for(int i=1; i<11; i++){
[ :p | expr ] code block with a parameter
(15 between: 1 and: 2 + 4 * 3) not false Transcript show: i ; cr ] System.out.println(i); }
<unary> method annotation
<key:’any’ wrd:#lit > with any literal arguments Messages + and * are sent first, then between:and: is sent, | names | String [] names ={"A", "B", "C"};
^ expr caret - return/answer a result and not. The rule suffers no exception: operators are just names := #(’A’ ’B’ ’C’). for( String name : names ) {
from a method binary messages with no notion of mathematical precedence. names do: [ :each | System.out.print( name );
2 + 4 * 3 reads left-to-right and gives 18, not 14! Transcript show: each, ’ , ’ ] System.out.print(","); }

Message Sending Collections start at 1.


aCol at:i accesses element at i and
Cascade: Sending Muliple Messages to the Same Object
When we send a message to an object (the receiver), the cor- aCol at:i put:value sets element at i to value.
Using ; (a cascade) multiple messages are sent to the result of
responding method is selected and executed, and the method
the same expression. Here ; arrives after add: 1, so messages Collections
answers an object. Message syntax mimics natural languages,
add: 2 and add: 3 are sent to add: 1’s receiver: a collection.
with a subject, a verb, and complements. #(4 2 1) at: 3 1
OrderedCollection new
#(4 2 1) copy at: 3 put: 6 #(4 2 6)
Pharo Java
add: 1;
add: 2; {4 . 2 . 1} at: 3 put: 6 #(4 2 6)
aColor r: 0.2 g: 0.3 b: 0 aColor.setRGB(0.2,0.3,0) add: 3. (Array new: 2) add: 4; add: 2 ; yourself #(4 2)
d at: ’1’ put: ’Chocolate’. d.put("1", "Chocolate"); The whole message cascade value is the value of the last mes- Set new add: 4; add: 4 ; yourself aSet(4)
sage sent (here 3). To return the receiver of the message cas- Dictionary new
Three Types of Messages: Unary, Binary, and Keyword cade instead (i.e., the collection), send yourself as the last at: #a put: ’Alpha’ ; yourself a Dictionary(#a-
A unary message has no arguments. message of the cascade. >’Alpha’)
Blocks
Array new. anArray
#(4 2 1) size. 3 Blocks are objects containing code that is executed on demand.
They are the basis for control structures: conditionals & loops.
new is an unary message sent to classes (classes are objects).
2 = 2
A binary message takes only one argument and is named by ifTrue: [ Error signal: ’Help’ ].
one or more symbol characters from +, -, *, =, <, >, ... Send the message ifTrue: to the boolean true (computed

You might also like