Pharo Syntax in a Nutshell

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

Getting a Feel About Syntax

Give you the general feel to get started:

This lecture is an overview

No stress if you do not get it right now!

We will repeat in future lectures

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."
  <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 obtain a graphical element that we open in a window by sending it the message openInWorld

Getting the Pharo Logo from the Web

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

Syntactic Elements

comment "a comment"
character $c $# $@
string 'lulu' 'l''idiot'
symbol (unique string) #mac #+
literal array #(12 23 36)
integer 1, 2r101
real 1.5 6.03e-34,4, 2.4e7
boolean true, false
(instances of True and False)
undefined nil
(instance of UndefinedObject)
point 10@120

Essential Constructs

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

Essence of Pharo Computation

Three Kinds of Messages to Minimize Parentheses

Message Precedence

(Msg) > Unary > Binary > Keywords

This order minimizes () needs

But let us start with messages

Sending an Unary Message

receiver selector

Example

10000 factorial

We send the message factorial to the object 10000

Sending a Binary Message

receiver selector argument

Example

1 + 3

We send the message + to the object 1 with the object 3 as argument

Sending a Keyword Message

receiver keyword1: arg1 keyword2: arg2

equivalent to C like syntax

receiver.keyword1keyword2(arg1, 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

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'

You can 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 to perform the iteration on itself

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

Blocks Look like Functions

fct(x) = x*x+3

fct := [ :x | x * x + 3 ]

fct(2)

fct value: 2

Blocks

[ :each | Transcript show: each abs printString ; cr ]

Block Usage

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

Class Definition Template

Class Definition within the IDE

Method Definition

messageSelectorAndArgumentNames
   "comment stating purpose of message"

  | temporary variable names |
  statements

Method Definition Example

Messages Summary

3 kinds of messages:

Message Priority:

Conclusion

/