﻿1
00:00:00,760 --> 00:00:04,120
Hello. In this session,
we will talk about class methods.

2
00:00:05,320 --> 00:00:07,960
In Pharo, everything is an object,

3
00:00:08,120 --> 00:00:10,400
and you can send messages to objects.

4
00:00:11,120 --> 00:00:14,320
So if you take these two pieces
of information,

5
00:00:14,480 --> 00:00:16,160
classes are objects too,

6
00:00:16,320 --> 00:00:19,880
and you can send information,
hence messages to classes.

7
00:00:21,120 --> 00:00:22,800
I'll give you two examples.

8
00:00:22,960 --> 00:00:27,480
Time now: you send the message now
to the object Time which is a class.

9
00:00:27,640 --> 00:00:29,800
And it returns the current time.

10
00:00:31,160 --> 00:00:32,680
So I send the message now

11
00:00:33,680 --> 00:00:37,320
to the object Time
which is a class name.

12
00:00:38,360 --> 00:00:40,320
It sends me back the current time.

13
00:00:40,480 --> 00:00:45,440
Likewise, I send the message today
to the object Date which is a class;

14
00:00:45,600 --> 00:00:47,400
it returns the date.

15
00:00:47,560 --> 00:00:49,480
I'll give three more examples.

16
00:00:50,200 --> 00:00:54,440
I send the message workingDirectory
to the class FileLocator;

17
00:00:54,600 --> 00:00:58,040
it returns the current path,

18
00:00:58,200 --> 00:01:02,520
the file where the execution
takes place.

19
00:01:03,720 --> 00:01:05,080
In the second example,

20
00:01:05,240 --> 00:01:08,760
I send the message getPng
to the object ZnEasy

21
00:01:08,920 --> 00:01:13,000
with the URL of a png file
as the argument.

22
00:01:13,160 --> 00:01:17,160
It returns the png file
which was previously downloaded.

23
00:01:18,600 --> 00:01:19,920
In the third example,

24
00:01:20,080 --> 00:01:24,000
I send the message startDefaultOn
to ZnServer which is a class again,

25
00:01:24,160 --> 00:01:25,800
with a port number.

26
00:01:27,160 --> 00:01:30,280
This will start the HTTP server.

27
00:01:31,480 --> 00:01:35,360
So in the three cases,
you send a message to one class.

28
00:01:36,320 --> 00:01:40,200
How do you implement a class method?

29
00:01:41,000 --> 00:01:42,560
You select the class

30
00:01:43,960 --> 00:01:47,880
and click on the button Class
to say you want to implement a method

31
00:01:48,040 --> 00:01:50,960
on the class side
and not on the instance side.

32
00:01:51,120 --> 00:01:53,920
And you implement a method like usual.

33
00:01:54,800 --> 00:01:58,880
So the method today is implemented
on the class side,

34
00:01:59,040 --> 00:02:00,880
in the class Date.

35
00:02:03,440 --> 00:02:07,080
On this slide,
you can see a common mistake.

36
00:02:07,240 --> 00:02:12,760
Here, you wish to send
the message withValue

37
00:02:12,920 --> 00:02:14,920
to the class Counter;

38
00:02:15,080 --> 00:02:19,040
to return a new Counter
with a value set as a parameter.

39
00:02:19,200 --> 00:02:20,800
So you want

40
00:02:22,520 --> 00:02:26,840
Counter withValue: 10 to return a new
Counter which starts with value 10.

41
00:02:28,280 --> 00:02:32,560
If I evaluate this expression,
I get the value Counter,

42
00:02:32,720 --> 00:02:34,240
not a new counter.

43
00:02:34,400 --> 00:02:38,240
In fact, I get the class,
not a new instance of this class.

44
00:02:38,400 --> 00:02:41,320
Why this mistake? What's the problem?

45
00:02:41,480 --> 00:02:46,320
The problem is that by default,
all methods return self.

46
00:02:47,280 --> 00:02:51,560
So if I don't specify the value
to return, the method will return self.

47
00:02:51,720 --> 00:02:56,160
So the code up here is equivalent
to the code down there.

48
00:02:56,320 --> 00:02:59,320
In this case, self is the class Counter.

49
00:03:00,640 --> 00:03:05,640
So the method returns the class itself,
not the instance created just above.

50
00:03:05,800 --> 00:03:07,640
To correct this problem,

51
00:03:07,800 --> 00:03:11,680
simply add a caret

52
00:03:11,840 --> 00:03:13,320
in front of self new.

53
00:03:14,240 --> 00:03:16,880
To summarize, classes are objects.

54
00:03:17,040 --> 00:03:21,560
You can send messages to any objects,
so you can send them to any classes too.

55
00:03:21,720 --> 00:03:24,360
To implement the corresponding methods,

56
00:03:24,520 --> 00:03:29,000
you need to press the button Class
after selecting a class.

57
00:03:30,000 --> 00:03:33,160
Most class methods create
not only new instances

58
00:03:33,320 --> 00:03:36,000
but also other things.

59
00:03:36,160 --> 00:03:40,480
You will see lots of other uses
of class methods in Pharo.

60
00:03:40,640 --> 00:03:44,800
Class methods are just
like other methods;

61
00:03:44,960 --> 00:03:46,800
there are no particular rules.

62
00:03:46,960 --> 00:03:49,960
The lookup works
exactly in the same way.

63
00:03:50,120 --> 00:03:52,160
We will learn about it next week.