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
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' 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
(ZnEasy getPng: 'http://pharo.org/web/files/pharo.png')
asMorph openInWindow
ZnEasy
designates a classgetPng:
is sent to the ZnEasy
class with a string as argumentgetPng:
is a keyword message 'http://pharo.org/web/files/pharo.png'
is a stringasMorph
and openInWindow
are from left to rightcomment | "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 |
| var |
var := aValue
message . message
^ expression
[ :x | x + 2 ] value: 5
> 7
receiver selector
9 squared
Date today
receiver selector argument
1+2
3@4
receiver key1: arg1 key2: arg2
2 between: 10 and: 20
(Msg) > Unary > Binary > Keywords
()
This order minimizes ()
needs
But let us start with messages
receiver selector
Example
10000 factorial
We send the message factorial
to the object 10000
receiver selector argument
Example
1 + 3
We send the message +
to the object 1
with the object 3
as argument
receiver keyword1: arg1 keyword2: arg2
equivalent to C like syntax
receiver.keyword1keyword2(arg1, arg2)
ZnClient new
url: 'https://en.wikipedia.org/w/index.php';
queryAt: 'title' put: 'Pharo';
queryAt: 'action' put: 'edit';
get
new
is a unary message sent to a classqueryAt:put:
is a keyword messageget
is a unary message;
(called a cascade) sends all messages to the same receiverfactorial
"Answer the factorial of the receiver."
self = 0 ifTrue: [ ^ 1 ].
self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
self error: 'Not valid for negative integers'
ifTrue:
is sent to an object, a boolean!ifFalse:ifTrue:
, ifTrue:ifFalse:
and ifFalse:
also existYou can read their implementation, this is not magic!
1 to: 4 do: [ :i| Transcript << i ]
> 1
> 2
> 3
> 4
to:do:
is a message sent to an integertimesRepeat:
, to:by:do:
, whileTrue:
, whileFalse:
, ...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
fct(x) = x*x+3
fct := [ :x | x * x + 3 ]
fct(2)
fct value: 2
[ :each | Transcript show: each abs printString ; cr ]
#(1 2 -4 -86)
do: [ :each | Transcript show: each abs printString ; cr ]
> 1
> 2
> 4
> 86
[ ]
delimits the block:each
is the block argumenteach
will take the value of each element of the array
self
messageSelectorAndArgumentNames
"comment stating purpose of message"
| temporary variable names |
statements
3 kinds of messages:
Node new
1+2
, 3@4
2 between: 10 and: 20
Message Priority:
/