Pharo Syntax in a Nutshell

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Getting a Feel About Syntax

In this lecture we want to give you the general feel to get started:

No stress if you do not get it right now!

The Complete Syntax on a Postcard

No need to understand everything! But everything is on this screen :)

exampleWithNumber: x
  "This method illustrates the complete syntax except primitive invocation."
  <aMethodAnnotation>

  | y |
  true & false not & (nil isNil)
    ifFalse: [ self halt ].
  y := self size + super size.
  #($a #a 'a' 1 1.0)
    do: [ :each | Transcript
            show: (each class name);
            show: (each printString);
            show: ' ' ].
  ^ x < y

Hello World

	'Hello World' asMorph openInWindow

We send the message asMorph to a string and get a graphical element that we open in a window

Getting the Pharo Logo

  (ZnEasy getPng: 'http://pharo.org/web/files/pharo.png')
    asMorph openInWindow.

Sending an unary message

	receiver selector

Example

	10000 factorial

We send the message factorial to the object 10000

A Simple Method

factorial
  "Answer the factorial of the receiver."

  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'

Within the Browser part of the Pharo IDE

A Simple Method

factorial
  "Answer the factorial of the receiver."

  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'

Presentation Convention

factorial
  "Answer the factorial of the receiver."
  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'

is displayed in this lecture as:

Integer >> factorial
  "Answer the factorial of the receiver."
  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'

Sending a keyword message

	receiver keyword1: arg1 keyword2: arg2

Example: Sending an HTTP Request

ZnClient new
  url: 'https://en.wikipedia.org/w/index.php';
  queryAt: 'title' put: 'Pharo';
  queryAt: 'action'  put: 'edit';
  get

Essential Constructs

To be explained later:

  [ :x | x + 2 ] value: 5
  > 7 

Messages

To minimize the use of parentheses, Pharo distinguishes 3 kinds of messages

Messages

Messages are everywhere:

Conditionals are also Message Sends

factorial
  "Answer the factorial of the receiver."
  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'

Read their implementation, this is not magic!

Loops are also Message Sends

1 to: 4 do: [ :i| Transcript << i ]
> 1
> 2
> 3
> 4

With Iterators

We ask the collection object to perform the iteration

#(1 2 -4 -86)
  do: [ :each | Transcript show: each abs printString ; cr ]
> 1
> 2
> 4
> 86

Messages and Their Composition

3 kinds of messages:

Message Priority:

Message Examples

  (ZnEasy getPng: 'http://pharo.org/web/files/pharo.png')
    asMorph openInWindow.

Blocks

Block Usage

  #(1 2 -4 -86) 
    do: [ :each | Transcript show: each abs printString ; cr ]
  > 1
  > 2
  > 4
  > 86

Class Definition

Class Definition with the IDE

Class Definition with the IDE

Object subclass: #Point
  instanceVariableNames: 'x y'
  classVariableNames: ''
  category: 'Graphics'

Method Definition

factorial
  "Answer the factorial of the receiver."

  self = 0 ifTrue: [ ^ 1 ].
  self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
  self error: 'Not valid for negative integers'

Method Definition

Conclusion

/