﻿1
00:00:00,040 --> 00:00:02,880
This will be an overview
of the syntax.

2
00:00:03,040 --> 00:00:06,240
We'll show you
the entirety of the syntax...

3
00:00:06,400 --> 00:00:09,440
Don't get stressed
if you don't understand it completely.

4
00:00:09,600 --> 00:00:11,400
We'll go over it all again.

5
00:00:11,560 --> 00:00:15,360
The idea is to show you
all there is in Pharo.

6
00:00:16,040 --> 00:00:20,000
We're going to look
at the main syntactical elements.

7
00:00:20,160 --> 00:00:22,760
We'll see that there are
3 kinds of messages.

8
00:00:22,920 --> 00:00:25,280
And well look at block syntax,

9
00:00:25,440 --> 00:00:27,440
an essential part of Pharo.

10
00:00:27,600 --> 00:00:32,000
Don't worry if you don't get it all,
we'll repeat all the points.

11
00:00:33,120 --> 00:00:37,320
Just to remind you,
you have all the syntax on this slide.

12
00:00:37,480 --> 00:00:40,040
I can go into the details a bit now.

13
00:00:40,200 --> 00:00:43,960
Here's a method definition,
exampleWithNumber.

14
00:00:44,120 --> 00:00:46,120
And it has an argument.

15
00:00:46,360 --> 00:00:51,040
I have local variable definitions
with vertical bars.

16
00:00:51,200 --> 00:00:54,040
I have the return,
which lets me give a value.

17
00:00:54,200 --> 00:00:57,400
I have loops, an affectation...

18
00:00:57,560 --> 00:01:01,640
The period, for example,
is an instruction separator.

19
00:01:03,120 --> 00:01:05,600
We'll look at all this.

20
00:01:07,040 --> 00:01:10,080
We'll start
with the traditional Hello World.

21
00:01:10,240 --> 00:01:12,000
Here, if you like...

22
00:01:12,160 --> 00:01:16,400
You can execute all instructions
in Pharo yourself

23
00:01:16,560 --> 00:01:18,360
to see what the result is.

24
00:01:18,520 --> 00:01:21,440
So here I have a string,

25
00:01:21,840 --> 00:01:25,880
which I convert into a morph,
a graphic element,

26
00:01:26,040 --> 00:01:29,640
and I ask it
to open in a window.

27
00:01:29,840 --> 00:01:33,320
So I send asMorph
to the string object.

28
00:01:33,480 --> 00:01:36,080
And I send openInWindow

29
00:01:36,240 --> 00:01:37,920
to the resulting morph.

30
00:01:38,080 --> 00:01:41,480
So we have two message sends
and one string object.

31
00:01:42,520 --> 00:01:43,960
A more complicated example.

32
00:01:44,120 --> 00:01:47,920
Imagine you want to take
the Pharo logo from the web.

33
00:01:48,280 --> 00:01:50,800
Well, you do it this way.

34
00:01:50,960 --> 00:01:56,840
You do ZnEasy getPng:
with a string which represents the URL

35
00:01:57,000 --> 00:02:00,000
of the resource you want to read.

36
00:02:00,160 --> 00:02:04,440
And once again you convert it
into a graphic object

37
00:02:04,600 --> 00:02:06,760
and ask it to open in a window.

38
00:02:06,920 --> 00:02:09,200
So we see that ZnEasy is a class

39
00:02:09,360 --> 00:02:12,480
because classes start with a capital.

40
00:02:12,640 --> 00:02:17,800
The colon of the message getPng:
means I'm expecting an argument,

41
00:02:17,960 --> 00:02:22,120
which is the string
http://pharo.org.

42
00:02:22,280 --> 00:02:27,120
This string is the argument
of the method getPng:

43
00:02:28,280 --> 00:02:32,520
The messages asMorph
and openInWindow

44
00:02:32,680 --> 00:02:35,400
are read from left to right.

45
00:02:35,800 --> 00:02:38,640
So you can cut'n'paste this code

46
00:02:38,800 --> 00:02:41,000
or retype it in Pharo,

47
00:02:41,160 --> 00:02:43,760
and you'll get the Pharo logo.

48
00:02:43,920 --> 00:02:47,200
So what syntactical elements
are there in Pharo?

49
00:02:47,360 --> 00:02:50,680
You have comments, which start
with double quotation marks.

50
00:02:50,840 --> 00:02:52,200
That's a comment.

51
00:02:52,360 --> 00:02:55,840
Characters are always preceded
by a dollar sign.

52
00:02:56,000 --> 00:02:58,080
$A is the character A.

53
00:02:58,240 --> 00:03:02,280
Strings start and finish
with a single quotation mark.

54
00:03:03,400 --> 00:03:05,920
You have symbols,
which are unique strings.

55
00:03:06,080 --> 00:03:10,320
These begin like this,
with the hashtag.

56
00:03:10,480 --> 00:03:12,840
#a is the symbol a.

57
00:03:13,000 --> 00:03:14,600
There are literal arrays.

58
00:03:14,760 --> 00:03:19,200
Here I have one with 12, 23 and 36.
Three elements.

59
00:03:19,560 --> 00:03:22,280
Integers have various representations,

60
00:03:22,440 --> 00:03:24,600
but we won't dwell on this.

61
00:03:24,760 --> 00:03:27,360
We usually use representations
in base 10.

62
00:03:27,520 --> 00:03:30,240
But if you want one in base 2,
that's OK.

63
00:03:30,400 --> 00:03:32,760
You have reals.
This is the same.

64
00:03:32,920 --> 00:03:35,200
You can find the syntax if you need.

65
00:03:35,360 --> 00:03:38,080
You have booleans,
with true and false.

66
00:03:38,240 --> 00:03:39,800
You have undefined.

67
00:03:39,960 --> 00:03:43,640
The null of Java is nil in Pharo.

68
00:03:43,800 --> 00:03:47,200
It's an instance
of the class UndefinedObject.

69
00:03:47,480 --> 00:03:49,320
And you have points.

70
00:03:49,480 --> 00:03:52,720
This is described in Pharo
under the form

71
00:03:52,880 --> 00:03:56,440
of 10 @ 200, for example.

72
00:03:56,600 --> 00:03:59,880
So I'm creating the point 10,
which is 10 on the x,

73
00:04:00,040 --> 00:04:02,440
and 200 on the y.

74
00:04:03,680 --> 00:04:07,040
So we've seen the essential elements
for the syntax.

75
00:04:07,200 --> 00:04:09,840
Now we have the constructs
of the language.

76
00:04:10,000 --> 00:04:13,040
You have
temporary variable declaration.

77
00:04:13,200 --> 00:04:16,200
It's a vertical bar
followed by another vertical bar.

78
00:04:16,360 --> 00:04:19,320
Assignment
is a colon and an equal sign.

79
00:04:19,480 --> 00:04:22,520
In the variable var I put aValue.

80
00:04:22,720 --> 00:04:24,640
I have messages in my system.

81
00:04:24,800 --> 00:04:26,280
How do I separate them?

82
00:04:26,440 --> 00:04:28,240
With a period.

83
00:04:28,400 --> 00:04:30,760
When I want a sequence of messages.

84
00:04:30,920 --> 00:04:32,000
There's return,

85
00:04:32,160 --> 00:04:34,560
where a function or method
gives a value.

86
00:04:34,720 --> 00:04:39,520
I use the little hat,
which is called caret in English.

87
00:04:39,680 --> 00:04:41,200
Then you have blocks,

88
00:04:41,360 --> 00:04:45,760
which, technically speaking,
are lexical closures,

89
00:04:46,480 --> 00:04:49,840
and which you can take
as anonymous methods.

90
00:04:50,000 --> 00:04:53,120
I'll show you what a block is.

91
00:04:53,280 --> 00:04:55,880
It's defined with square brackets.

92
00:04:56,680 --> 00:04:59,560
And it's executed
with the message value.

93
00:04:59,720 --> 00:05:01,160
We'll look at this.

94
00:05:01,320 --> 00:05:03,960
There you have all the constructs.

95
00:05:05,240 --> 00:05:08,520
So what is the essence
of a Pharo program?

96
00:05:08,680 --> 00:05:12,240
It's creating objects
by using messages,

97
00:05:12,400 --> 00:05:14,320
sending messages to these objects,

98
00:05:14,480 --> 00:05:17,280
and, now and then,
using blocks, or anonymous methods.

99
00:05:17,440 --> 00:05:18,680
That's all.

100
00:05:19,800 --> 00:05:23,080
There are three types
of message in Pharo. Why?

101
00:05:23,240 --> 00:05:26,560
In order to minimize
the number of parentheses used.

102
00:05:26,720 --> 00:05:28,320
It's fairly simple.

103
00:05:28,480 --> 00:05:32,360
A unary message
is always a receiver and a selector.

104
00:05:32,520 --> 00:05:35,480
Here for example I've said
that I want 9 squared.

105
00:05:35,640 --> 00:05:37,680
I want today's date.

106
00:05:37,840 --> 00:05:41,480
I send the message today
to the class Date.

107
00:05:41,960 --> 00:05:43,800
There's no argument.

108
00:05:43,960 --> 00:05:46,080
Then there's the binary message,

109
00:05:46,240 --> 00:05:50,800
which also takes the form
of a receiver, selector and argument.

110
00:05:51,160 --> 00:05:52,640
But the selector will be

111
00:05:52,800 --> 00:05:57,320
plus, minus, tilde, equals, slash...
That sort of thing.

112
00:05:57,480 --> 00:06:01,080
All those mathematical signs
are for binary messages.

113
00:06:01,360 --> 00:06:04,560
Here we see 1+2.
This is a message.

114
00:06:04,720 --> 00:06:08,080
Sending message + to object 1
with argument object 2.

115
00:06:08,240 --> 00:06:11,240
@ for the creation of a point
is also a message,

116
00:06:11,400 --> 00:06:12,760
a binary message.

117
00:06:12,920 --> 00:06:16,160
Here I create the point 3@4

118
00:06:16,320 --> 00:06:18,640
by sending the message @.

119
00:06:18,800 --> 00:06:22,200
Then you have
what we call keyword messages.

120
00:06:22,360 --> 00:06:24,920
A keyword message takes the form

121
00:06:25,080 --> 00:06:28,840
of receiver key1: arg1 key2: arg2.

122
00:06:29,000 --> 00:06:31,440
So the example is...

123
00:06:31,600 --> 00:06:34,720
is 2 between 10 and 20

124
00:06:35,000 --> 00:06:38,760
If you wrote it in Java or C
it would be like this...

125
00:06:39,000 --> 00:06:44,320
You'd write betweenAnd...

126
00:06:44,480 --> 00:06:48,920
Then, between brackets,
you'd write (10, 20).

127
00:06:49,080 --> 00:06:53,720
In Pharo you express it
in the form of key and key.

128
00:06:53,880 --> 00:06:57,520
We'll look at this again, but it gives
you an idea of the syntax.

129
00:06:58,800 --> 00:07:02,680
There's a precedence
between messages,

130
00:07:02,840 --> 00:07:06,480
the system will execute in priority
the parentheses,

131
00:07:06,640 --> 00:07:09,200
then the unary,
binary and keyword messages.

132
00:07:09,360 --> 00:07:10,160
Why?

133
00:07:10,320 --> 00:07:13,560
To avoid writing too many parentheses.

134
00:07:13,720 --> 00:07:17,880
We could have had a system
with parentheses all over the place,

135
00:07:18,040 --> 00:07:20,000
but the code would be illegible.

136
00:07:20,960 --> 00:07:24,680
I'll go back over each type of message
in a simple way.

137
00:07:24,840 --> 00:07:27,680
This unary message, 10000 factorial.

138
00:07:27,840 --> 00:07:30,160
Try it, it's a very big number.

139
00:07:30,320 --> 00:07:33,640
I send the message factorial
to the object 10000.

140
00:07:33,800 --> 00:07:36,240
There's no argument,
it's a unary message.

141
00:07:36,880 --> 00:07:41,840
Similarly, a binary message.
1+3 is a message, a binary message.

142
00:07:42,000 --> 00:07:46,720
The selector,
the name of the method, is +.

143
00:07:47,360 --> 00:07:52,040
I send the message + to the object 1
with the object 3 as argument.

144
00:07:53,120 --> 00:07:55,960
A keyword message,
as I explained earlier...

145
00:07:56,120 --> 00:08:00,360
I'll just tell you again.
Teaching is all about repetition.

146
00:08:00,800 --> 00:08:04,520
I have a keyword message
with keyword and keyword,

147
00:08:04,680 --> 00:08:07,560
which is the message,
with the arguments,

148
00:08:07,720 --> 00:08:11,080
which are floating inside the message.

149
00:08:11,240 --> 00:08:14,680
The idea is to have a syntax
that children can use

150
00:08:14,840 --> 00:08:18,320
and to have something
close to English.

151
00:08:18,480 --> 00:08:24,280
So you can introduce the arguments
inside your messages.

152
00:08:25,440 --> 00:08:29,120
As an example, imagine
I'm sending an HTTP request.

153
00:08:29,280 --> 00:08:32,880
ZnClient is a Pharo HTTP server.

154
00:08:33,040 --> 00:08:35,880
So I create an instance of Zn server.

155
00:08:36,040 --> 00:08:40,680
So new is a unary message,
there's no argument, no colon.

156
00:08:41,560 --> 00:08:46,720
url is a keyword message,
there's a colon.

157
00:08:46,880 --> 00:08:49,880
queryAt: put:
is another message.

158
00:08:50,040 --> 00:08:52,600
It's another keyword message.

159
00:08:52,760 --> 00:08:54,880
And get is a unary message.

160
00:08:55,360 --> 00:08:59,400
What we can also see
in this example are semi-colons.

161
00:08:59,760 --> 00:09:02,120
We call this a cascade,

162
00:09:02,880 --> 00:09:05,800
which sends all the messages
to the same object.

163
00:09:05,960 --> 00:09:08,920
You don't need
a temporary variable.

164
00:09:09,080 --> 00:09:12,320
And you can write more compact code.

165
00:09:13,720 --> 00:09:17,080
You have to understand
that messages are everywhere.

166
00:09:17,240 --> 00:09:20,880
Conditionals, loops, iterators and
concurrency are expressed as messages.

167
00:09:21,040 --> 00:09:23,480
My explanation
about those three messages

168
00:09:23,640 --> 00:09:25,560
applies across the board.

169
00:09:25,720 --> 00:09:27,760
It's the cornerstone of the system.

170
00:09:27,920 --> 00:09:31,000
There's nothing other
than these messages.

171
00:09:31,520 --> 00:09:32,880
Let's have a look.

172
00:09:33,040 --> 00:09:35,520
I've taken
the silly factorial definition here.

173
00:09:35,680 --> 00:09:37,200
How do I define it?

174
00:09:37,360 --> 00:09:40,160
I define my method factorial.

175
00:09:40,320 --> 00:09:43,840
I put a comment for the method.
Double quotation marks.

176
00:09:44,000 --> 00:09:45,360
And I start with self.

177
00:09:45,520 --> 00:09:47,560
If the object
getting the message is zero,

178
00:09:47,720 --> 00:09:49,600
then I'll give 1.

179
00:09:49,760 --> 00:09:51,320
If self is positive,

180
00:09:51,480 --> 00:09:55,160
for example, factorial 6,
self equals 6...

181
00:09:55,480 --> 00:09:58,120
If this is true
than what will I give?

182
00:09:58,280 --> 00:10:01,520
6 multiplied by 6 minus 1 factorial.

183
00:10:01,680 --> 00:10:04,920
The recursive definition we all know.
And that's that.

184
00:10:05,080 --> 00:10:08,120
And the interesting thing to note here

185
00:10:08,280 --> 00:10:10,440
is that ifTrue:, which is a condition,

186
00:10:10,600 --> 00:10:13,720
is also a message,
a keyword message with a colon,

187
00:10:13,880 --> 00:10:15,320
sent to a boolean.

188
00:10:15,480 --> 00:10:18,800
You also have ifFalse:ifTrue:,
ifTrue:ifFalse:...

189
00:10:18,960 --> 00:10:21,440
All these conditions are messages

190
00:10:21,600 --> 00:10:23,320
which satisfy the rules,

191
00:10:23,480 --> 00:10:28,680
and are keyword messages
like I presented earlier.

192
00:10:29,080 --> 00:10:30,960
So conditions are messages.

193
00:10:31,120 --> 00:10:32,960
Loops are messages too.

194
00:10:33,120 --> 00:10:35,160
Here I'll make a loop from 1 to 4.

195
00:10:35,320 --> 00:10:37,840
From 1 to 4, what do I do?

196
00:10:38,000 --> 00:10:40,080
A block.
An anonymous method.

197
00:10:40,240 --> 00:10:45,360
Here I'm saying I want it
to show the value of i,

198
00:10:45,520 --> 00:10:50,320
and i will take the value
of 1, 2, 3, 4...

199
00:10:50,480 --> 00:10:53,840
And there you can see that to: do:

200
00:10:54,000 --> 00:10:56,280
is a keyword message.

201
00:10:56,520 --> 00:10:59,960
It's a message
that takes the form of a loop.

202
00:11:00,120 --> 00:11:03,360
There are other loops in Pharo:
timesRepeat:, to:by:do: and so on.

203
00:11:03,520 --> 00:11:05,360
We'll look at them all.

204
00:11:05,520 --> 00:11:07,520
But, again, these are messages...

205
00:11:07,680 --> 00:11:11,440
So here, to: do: is sent to object 1
with 4 as parameter and a block.

206
00:11:11,600 --> 00:11:14,600
We'll see what a block is very soon.

207
00:11:14,760 --> 00:11:16,480
You also have iterators.

208
00:11:16,640 --> 00:11:18,440
Iterators are different.

209
00:11:18,600 --> 00:11:21,040
I send the message do:

210
00:11:21,200 --> 00:11:22,360
to the collection,

211
00:11:22,520 --> 00:11:25,680
saying what I want to apply
to each of the elements.

212
00:11:25,840 --> 00:11:29,320
So the each,
the argument of my function,

213
00:11:29,480 --> 00:11:34,440
will take as value
1, 2, -4 and -86.

214
00:11:34,600 --> 00:11:36,960
And what I do here
is that the each...

215
00:11:38,440 --> 00:11:40,800
I apply the absolute value
and display it.

216
00:11:40,960 --> 00:11:43,840
Once again,
this will be another course.

217
00:11:44,000 --> 00:11:45,720
We'll explain iterators.

218
00:11:45,880 --> 00:11:48,640
Iterators are something
you get in Java 8.

219
00:11:50,280 --> 00:11:53,240
And they're part of the core of Pharo.

220
00:11:54,280 --> 00:11:55,760
So what is a block?

221
00:11:55,920 --> 00:11:57,680
Blocks look like functions.

222
00:11:57,840 --> 00:12:01,320
If in maths you wrote f(x) = x²+3,

223
00:12:01,480 --> 00:12:03,920
you'd write it like this, literally.

224
00:12:04,440 --> 00:12:07,320
I define a variable
in which I affect a block.

225
00:12:07,480 --> 00:12:10,520
A block is something
inside square brackets.

226
00:12:10,680 --> 00:12:13,160
Here, the block has an argument
which is x.

227
00:12:13,520 --> 00:12:16,640
And I put a core, which is x+3.

228
00:12:16,800 --> 00:12:18,600
And when I want
to execute this block...

229
00:12:18,760 --> 00:12:20,880
The application of the function.

230
00:12:21,040 --> 00:12:24,600
That's the definition of the function,
now I have the application.

231
00:12:24,760 --> 00:12:30,360
I send the message value:
to the block. I put value: 2.

232
00:12:31,520 --> 00:12:35,080
x is 2,
so my block will give me 5.

233
00:12:35,520 --> 00:12:36,680
OK?

234
00:12:36,960 --> 00:12:41,560
So what's a block? It's a kind
of anonymous function, it has no name.

235
00:12:41,960 --> 00:12:46,160
In fact, it's a lexical closure.

236
00:12:46,320 --> 00:12:48,240
We'll explain that.

237
00:12:48,400 --> 00:12:50,360
Blocks are real objects.

238
00:12:50,520 --> 00:12:54,440
They can be passed as method
arguments, stored in variables,

239
00:12:54,600 --> 00:12:55,720
returned...

240
00:12:55,880 --> 00:12:58,600
So they can be used
as button triggers,

241
00:12:58,760 --> 00:13:02,960
as callbacks in web applications...
These can be blocks.

242
00:13:04,480 --> 00:13:06,560
We've already seen block usage.

243
00:13:06,720 --> 00:13:08,800
In iterators you have blocks.

244
00:13:08,960 --> 00:13:11,680
We saw blocks were used
in conditions.

245
00:13:11,840 --> 00:13:13,600
There'll be a course about blocks.

246
00:13:13,840 --> 00:13:16,440
It's elegant,
it was introduced in Eiffel,

247
00:13:16,600 --> 00:13:19,200
it was recently introduced in Java 8,

248
00:13:19,360 --> 00:13:21,800
there was a first version in C#...

249
00:13:21,960 --> 00:13:23,200
So it's fundamental.

250
00:13:23,360 --> 00:13:26,320
Blocks are a fundamental element

251
00:13:26,480 --> 00:13:30,400
of Pharo's syntax
and computational model.

252
00:13:30,720 --> 00:13:33,600
How do we define a class in Pharo?

253
00:13:33,760 --> 00:13:37,480
The browser you see here
shows you packages.

254
00:13:37,640 --> 00:13:39,960
You have the classes inside.

255
00:13:40,120 --> 00:13:41,600
And it shows you a template.

256
00:13:41,760 --> 00:13:45,240
The template is saying that
by default you inherit an object,

257
00:13:45,400 --> 00:13:47,480
and the name of the class.

258
00:13:47,640 --> 00:13:51,160
We'll see it for the class Point,
for example.

259
00:13:51,320 --> 00:13:53,360
So the definition of the class...

260
00:13:53,520 --> 00:13:56,840
I have the class object.
It's also a message!

261
00:13:57,000 --> 00:13:59,680
The message subclass:
InstanceVariableNames:

262
00:13:59,840 --> 00:14:01,160
blah blah blah...

263
00:14:01,320 --> 00:14:02,160
With...

264
00:14:02,320 --> 00:14:04,960
I define the class Point,

265
00:14:05,120 --> 00:14:07,920
and I give the instance variables
x and y.

266
00:14:08,080 --> 00:14:13,640
That's just to show the syntax always
follows the same model with messages.

267
00:14:13,800 --> 00:14:16,680
You don't usually write it out
by hand.

268
00:14:16,840 --> 00:14:20,960
The tool gives you the template
and you just fill it in.

269
00:14:22,680 --> 00:14:24,480
So how do we define a method?

270
00:14:24,640 --> 00:14:27,400
As I said, the methods
are public and virtual,

271
00:14:27,560 --> 00:14:29,240
and by default return self.

272
00:14:29,400 --> 00:14:31,720
So, when you're in the browser,

273
00:14:31,880 --> 00:14:34,120
it suggests a template,

274
00:14:34,280 --> 00:14:37,160
the name of the potential method
and a comment...

275
00:14:37,320 --> 00:14:41,800
We're nice and polite
so we comment on our code.

276
00:14:41,960 --> 00:14:46,800
We can also put temporary
variable names and statements.

277
00:14:46,960 --> 00:14:50,360
What does this gives us?
We've seen the method factorial.

278
00:14:50,520 --> 00:14:53,040
Here I'm in the class Integer.

279
00:14:53,200 --> 00:14:56,400
I look at the method factorial,
I have its code,

280
00:14:56,560 --> 00:14:59,000
with the name of the method,
its comment

281
00:14:59,160 --> 00:15:02,040
and the core of the method
that I've defined.

282
00:15:03,840 --> 00:15:05,840
So, to sum up, what have we seen?

283
00:15:06,000 --> 00:15:09,560
There are 3 kinds of messages:
unary messages, without arguments,

284
00:15:09,720 --> 00:15:12,760
binary messages,
where the selectors are mathematical,

285
00:15:12,920 --> 00:15:17,000
and keyword messages,
where there can be several arguments.

286
00:15:17,160 --> 00:15:20,720
There's a priority:
parentheses first,

287
00:15:20,880 --> 00:15:23,080
then unary, binary and keyword.

288
00:15:23,240 --> 00:15:27,360
And if there are same-level messages
it's from left to right.

289
00:15:27,600 --> 00:15:30,920
What have we seen?
The syntax is very compact.

290
00:15:31,160 --> 00:15:35,040
There are few constructs
but they're really expressive.

291
00:15:35,200 --> 00:15:39,480
It's mainly messages
and lexical closures, or blocks.

292
00:15:39,640 --> 00:15:42,400
There are three kinds of messages.

293
00:15:42,560 --> 00:15:45,160
You'll do an exercise
to develop a language.

294
00:15:45,320 --> 00:15:49,200
Like that you can define what we call
Domain Specific Languages.