﻿WEBVTT

00:00:00.760 --> 00:00:04.120 align:middle
Hello. In this session,
we will talk about class methods.

00:00:05.320 --> 00:00:07.960 align:middle
In Pharo, everything is an object,

00:00:08.120 --> 00:00:10.400 align:middle
and you can send messages to objects.

00:00:11.120 --> 00:00:14.320 align:middle
So if you take these two pieces
of information,

00:00:14.480 --> 00:00:16.160 align:middle
classes are objects too,

00:00:16.320 --> 00:00:19.880 align:middle
and you can send information,
hence messages to classes.

00:00:21.120 --> 00:00:22.800 align:middle
I'll give you two examples.

00:00:22.960 --> 00:00:27.480 align:middle
Time now: you send the message now
to the object Time which is a class.

00:00:27.640 --> 00:00:29.800 align:middle
And it returns the current time.

00:00:31.160 --> 00:00:32.680 align:middle
So I send the message now

00:00:33.680 --> 00:00:37.320 align:middle
to the object Time
which is a class name.

00:00:38.360 --> 00:00:40.320 align:middle
It sends me back the current time.

00:00:40.480 --> 00:00:45.440 align:middle
Likewise, I send the message today
to the object Date which is a class;

00:00:45.600 --> 00:00:47.400 align:middle
it returns the date.

00:00:47.560 --> 00:00:49.480 align:middle
I'll give three more examples.

00:00:50.200 --> 00:00:54.440 align:middle
I send the message workingDirectory
to the class FileLocator;

00:00:54.600 --> 00:00:58.040 align:middle
it returns the current path,

00:00:58.200 --> 00:01:02.520 align:middle
the file where the execution
takes place.

00:01:03.720 --> 00:01:05.080 align:middle
In the second example,

00:01:05.240 --> 00:01:08.760 align:middle
I send the message getPng
to the object ZnEasy

00:01:08.920 --> 00:01:13.000 align:middle
with the URL of a png file
as the argument.

00:01:13.160 --> 00:01:17.160 align:middle
It returns the png file
which was previously downloaded.

00:01:18.600 --> 00:01:19.920 align:middle
In the third example,

00:01:20.080 --> 00:01:24.000 align:middle
I send the message startDefaultOn
to ZnServer which is a class again,

00:01:24.160 --> 00:01:25.800 align:middle
with a port number.

00:01:27.160 --> 00:01:30.280 align:middle
This will start the HTTP server.

00:01:31.480 --> 00:01:35.360 align:middle
So in the three cases,
you send a message to one class.

00:01:36.320 --> 00:01:40.200 align:middle
How do you implement a class method?

00:01:41.000 --> 00:01:42.560 align:middle
You select the class

00:01:43.960 --> 00:01:47.880 align:middle
and click on the button Class
to say you want to implement a method

00:01:48.040 --> 00:01:50.960 align:middle
on the class side
and not on the instance side.

00:01:51.120 --> 00:01:53.920 align:middle
And you implement a method like usual.

00:01:54.800 --> 00:01:58.880 align:middle
So the method today is implemented
on the class side,

00:01:59.040 --> 00:02:00.880 align:middle
in the class Date.

00:02:03.440 --> 00:02:07.080 align:middle
On this slide,
you can see a common mistake.

00:02:07.240 --> 00:02:12.760 align:middle
Here, you wish to send
the message withValue

00:02:12.920 --> 00:02:14.920 align:middle
to the class Counter;

00:02:15.080 --> 00:02:19.040 align:middle
to return a new Counter
with a value set as a parameter.

00:02:19.200 --> 00:02:20.800 align:middle
So you want

00:02:22.520 --> 00:02:26.840 align:middle
Counter withValue: 10 to return a new
Counter which starts with value 10.

00:02:28.280 --> 00:02:32.560 align:middle
If I evaluate this expression,
I get the value Counter,

00:02:32.720 --> 00:02:34.240 align:middle
not a new counter.

00:02:34.400 --> 00:02:38.240 align:middle
In fact, I get the class,
not a new instance of this class.

00:02:38.400 --> 00:02:41.320 align:middle
Why this mistake? What's the problem?

00:02:41.480 --> 00:02:46.320 align:middle
The problem is that by default,
all methods return self.

00:02:47.280 --> 00:02:51.560 align:middle
So if I don't specify the value
to return, the method will return self.

00:02:51.720 --> 00:02:56.160 align:middle
So the code up here is equivalent
to the code down there.

00:02:56.320 --> 00:02:59.320 align:middle
In this case, self is the class Counter.

00:03:00.640 --> 00:03:05.640 align:middle
So the method returns the class itself,
not the instance created just above.

00:03:05.800 --> 00:03:07.640 align:middle
To correct this problem,

00:03:07.800 --> 00:03:11.680 align:middle
simply add a caret

00:03:11.840 --> 00:03:13.320 align:middle
in front of self new.

00:03:14.240 --> 00:03:16.880 align:middle
To summarize, classes are objects.

00:03:17.040 --> 00:03:21.560 align:middle
You can send messages to any objects,
so you can send them to any classes too.

00:03:21.720 --> 00:03:24.360 align:middle
To implement the corresponding methods,

00:03:24.520 --> 00:03:29.000 align:middle
you need to press the button Class
after selecting a class.

00:03:30.000 --> 00:03:33.160 align:middle
Most class methods create
not only new instances

00:03:33.320 --> 00:03:36.000 align:middle
but also other things.

00:03:36.160 --> 00:03:40.480 align:middle
You will see lots of other uses
of class methods in Pharo.

00:03:40.640 --> 00:03:44.800 align:middle
Class methods are just
like other methods;

00:03:44.960 --> 00:03:46.800 align:middle
there are no particular rules.

00:03:46.960 --> 00:03:49.960 align:middle
The lookup works
exactly in the same way.

00:03:50.120 --> 00:03:52.160 align:middle
We will learn about it next week.