WEBVTT

00:00:00.480 --> 00:00:03.880 align:middle
Hello, this is a very advanced course.

00:00:04.720 --> 00:00:08.040 align:middle
We'll focus on
how Pharo organizes classes

00:00:08.760 --> 00:00:11.840 align:middle
and instance relationships
between classes.

00:00:12.400 --> 00:00:15.720 align:middle
It's really aimed at enthusiasts.

00:00:16.520 --> 00:00:21.600 align:middle
It's not essential to master,
since you already use Pharo without it.

00:00:22.000 --> 00:00:26.200 align:middle
You can use Pharo for programming
without knowing how it works.

00:00:26.400 --> 00:00:31.840 align:middle
But you may be unsatisfied if the course
didn't explain how it functions.

00:00:32.000 --> 00:00:34.640 align:middle
Like explaining where "new" is defined,

00:00:34.800 --> 00:00:37.400 align:middle
or what the class of a metaclass is.

00:00:37.920 --> 00:00:40.840 align:middle
This video will explain all this stuff.

00:00:41.040 --> 00:00:45.320 align:middle
It doesn't matter
if you don't understand straightaway.

00:00:45.400 --> 00:00:47.920 align:middle
But if it really interests you,

00:00:48.680 --> 00:00:50.840 align:middle
you can watch it several times.

00:00:51.720 --> 00:00:54.320 align:middle
There is one fundamental key here.

00:00:54.760 --> 00:00:59.400 align:middle
It's a fun aspect,
which this transparency should explain.

00:00:59.680 --> 00:01:00.680 align:middle
The basic idea,

00:01:00.840 --> 00:01:04.080 align:middle
which we keep reiterating in this MOOC,

00:01:04.200 --> 00:01:09.360 align:middle
is that, as you should know,
when we send a message to an object,

00:01:09.640 --> 00:01:13.560 align:middle
we search in the object's class
via the instantiation link.

00:01:13.760 --> 00:01:17.200 align:middle
If it isn't there,
we follow the inheritance chain.

00:01:17.400 --> 00:01:21.920 align:middle
We always start in the class,
then follow the inheritance chain.

00:01:22.400 --> 00:01:26.520 align:middle
This rule must be followed
systematically.

00:01:26.920 --> 00:01:29.400 align:middle
To explain metaclasses,

00:01:29.600 --> 00:01:34.840 align:middle
we summarize them in seven points,
which are relatively simple.

00:01:35.760 --> 00:01:38.600 align:middle
Every object is an instance of a class.

00:01:40.160 --> 00:01:43.080 align:middle
That's difficult!
Look at this example.

00:01:43.320 --> 00:01:47.200 align:middle
The object OrderedCollection
is an instance of this class.

00:01:47.600 --> 00:01:49.160 align:middle
So far, it's clear.

00:01:50.080 --> 00:01:55.200 align:middle
The second point is that every class
inherits from the Object class.

00:01:57.080 --> 00:01:58.720 align:middle
As we see here,

00:01:59.080 --> 00:02:03.840 align:middle
OrderedCollection and upwards
inherit ultimately from Object.

00:02:04.640 --> 00:02:07.040 align:middle
Just a note for French speakers,

00:02:07.200 --> 00:02:09.640 align:middle
"eventually" means "ultimately."

00:02:10.520 --> 00:02:11.560 align:middle
It's confusing.

00:02:11.800 --> 00:02:12.840 align:middle
So,

00:02:13.400 --> 00:02:18.040 align:middle
we have our inheritance graph
that inevitably leads to Object.

00:02:20.080 --> 00:02:25.000 align:middle
What is the responsibility of Object?
It represents object behavior

00:02:25.200 --> 00:02:28.560 align:middle
common to all objects,
such as error handling,

00:02:28.720 --> 00:02:31.080 align:middle
inserting a halt, and announcements.

00:02:31.680 --> 00:02:34.760 align:middle
Object is the root
of the inheritance tree.

00:02:35.360 --> 00:02:38.520 align:middle
Its key responsibility
is minimum behavior.

00:02:39.800 --> 00:02:44.200 align:middle
Returning to our list, every class
is an instance of a metaclass.

00:02:45.800 --> 00:02:48.520 align:middle
We explained this in an earlier course.

00:02:49.080 --> 00:02:53.520 align:middle
A class X is the sole instance
of the metaclass named X class.

00:02:53.680 --> 00:02:56.400 align:middle
We see that the OrderedCollection class

00:02:56.560 --> 00:03:00.040 align:middle
is an instance of the class
OrderedCollection class.

00:03:00.560 --> 00:03:05.040 align:middle
It works the same way
for the SequenceableCollection class,

00:03:05.560 --> 00:03:08.840 align:middle
and the Object class
with the class Object class.

00:03:09.080 --> 00:03:12.560 align:middle
All classes are instances
of another metaclass,

00:03:12.840 --> 00:03:16.280 align:middle
which takes the same name
followed by "class."

00:03:18.920 --> 00:03:20.640 align:middle
Metaclasses are created

00:03:20.840 --> 00:03:23.400 align:middle
automatically when we create a class.

00:03:23.600 --> 00:03:28.320 align:middle
We send a "new" message
to the metaclass to create the class.

00:03:30.080 --> 00:03:31.200 align:middle
Next point.

00:03:31.920 --> 00:03:35.400 align:middle
Metaclass hierarchy
parallels class hierarchy.

00:03:36.200 --> 00:03:39.160 align:middle
Earlier, we explained that

00:03:39.920 --> 00:03:43.200 align:middle
OrderedCollection
is an instance of its class,

00:03:43.400 --> 00:03:46.400 align:middle
and likewise for SequenceableCollection.

00:03:46.640 --> 00:03:51.080 align:middle
Now we need to specify
the relationship between the two.

00:03:51.280 --> 00:03:56.040 align:middle
Since there's an inheritance
relationship here, there's also one here.

00:03:56.920 --> 00:03:57.920 align:middle
In all cases.

00:03:58.840 --> 00:04:03.400 align:middle
That's why we say
they are two parallel hierarchies.

00:04:04.080 --> 00:04:06.320 align:middle
When do we use this hierarchy?

00:04:06.400 --> 00:04:10.080 align:middle
If I send the "new" message
to OrderedCollection,

00:04:10.520 --> 00:04:13.080 align:middle
where do I search?
In the class.

00:04:13.360 --> 00:04:18.920 align:middle
I query in each class
whether or not "new" has been defined.

00:04:19.200 --> 00:04:23.080 align:middle
Moving upwards, has it defined "new"?
Yes or no.

00:04:23.320 --> 00:04:28.400 align:middle
Here we utilize the key
I mentioned at the beginning.

00:04:28.840 --> 00:04:34.640 align:middle
To send a message, start with the class
then follow the inheritance chain.

00:04:35.360 --> 00:04:37.400 align:middle
Up to this point,

00:04:37.640 --> 00:04:41.400 align:middle
it may seem clear,
but now other questions come up.

00:04:41.600 --> 00:04:44.360 align:middle
What is Object class an instance of?

00:04:45.200 --> 00:04:46.400 align:middle
You might wonder.

00:04:46.840 --> 00:04:50.200 align:middle
And what is the superclass
of Object class?

00:04:51.000 --> 00:04:54.680 align:middle
Does it work
if we send a message to a metaclass?

00:04:56.160 --> 00:04:59.400 align:middle
Or if we send a message
to whatever it is here?

00:05:00.000 --> 00:05:01.600 align:middle
Let's have a look.

00:05:02.680 --> 00:05:07.800 align:middle
The system says that every metaclass
inherits from Class,

00:05:08.280 --> 00:05:10.080 align:middle
right up to Behavior.

00:05:11.000 --> 00:05:14.680 align:middle
Let's look at that,
and answer the first question.

00:05:14.840 --> 00:05:18.800 align:middle
Object class is a class,
hence it inherits from Class.

00:05:19.560 --> 00:05:23.320 align:middle
Class inherits
from ClassDescription and Behavior.

00:05:24.160 --> 00:05:28.040 align:middle
Unlike in Pharo,
in other systems such as Lisp

00:05:28.560 --> 00:05:31.200 align:middle
these two elements are a single class.

00:05:31.680 --> 00:05:33.640 align:middle
Pharo divides them for reuse.

00:05:33.840 --> 00:05:36.920 align:middle
Metaclass can be attached
to ClassDescription

00:05:37.080 --> 00:05:39.920 align:middle
to utilize ClassDescription
in two cases.

00:05:40.080 --> 00:05:44.400 align:middle
We see that Behavior,
which represents the essence of a class,

00:05:44.600 --> 00:05:46.720 align:middle
inherits from Object.

00:05:49.920 --> 00:05:53.000 align:middle
So, if I send a message here,

00:05:53.360 --> 00:05:56.040 align:middle
it will travel all the way up to here.

00:05:57.280 --> 00:06:00.840 align:middle
It follows the inheritance tree
from the Class level.

00:06:03.080 --> 00:06:06.200 align:middle
Now we want to know
where "new" is defined.

00:06:06.840 --> 00:06:10.840 align:middle
The method for creating instances
is defined in Behavior.

00:06:11.920 --> 00:06:17.680 align:middle
When I send the "new" message
to OrderedCollection, what happens?

00:06:18.080 --> 00:06:20.400 align:middle
First, I search in the class.

00:06:21.080 --> 00:06:24.920 align:middle
Suppose that "new" is not redefined
in the inheritance,

00:06:25.080 --> 00:06:28.000 align:middle
so I search all of the superclasses

00:06:28.360 --> 00:06:29.760 align:middle
until we find "new."

00:06:29.920 --> 00:06:34.600 align:middle
In a lookup, we search for a method
and execute it on the receiver.

00:06:34.840 --> 00:06:39.000 align:middle
So, I find the "new" method
and execute it on the receiver,

00:06:39.200 --> 00:06:44.200 align:middle
the class OrderedCollection,
which creates a new instance

00:06:44.360 --> 00:06:46.840 align:middle
that we'll name 3 4, for example.

00:06:47.720 --> 00:06:48.720 align:middle
Okay?

00:06:49.400 --> 00:06:52.320 align:middle
We're using the key I spoke about.

00:06:52.560 --> 00:06:57.840 align:middle
We search the inheritance chain,
starting with the receiver's class.

00:06:58.840 --> 00:07:01.920 align:middle
In a nutshell,
what does Behavior represent?

00:07:02.160 --> 00:07:05.840 align:middle
It's the essence of an object
that can have instances.

00:07:06.800 --> 00:07:08.520 align:middle
Objects with instances

00:07:09.760 --> 00:07:12.760 align:middle
must include a superclass link,

00:07:13.000 --> 00:07:16.920 align:middle
method dictionary,
and description of instances (format).

00:07:17.080 --> 00:07:22.840 align:middle
The methods shown here include
examples such as new, basicNew and new:.

00:07:23.400 --> 00:07:26.520 align:middle
The difference between new and basicNew

00:07:26.680 --> 00:07:29.800 align:middle
is that you must never
redefine basicNew.

00:07:30.080 --> 00:07:33.000 align:middle
As we explained
in the course on methods,

00:07:33.200 --> 00:07:38.760 align:middle
if you redefine a method starting
with "basic," the method is inaccessible.

00:07:39.600 --> 00:07:40.920 align:middle
The original method.

00:07:41.080 --> 00:07:46.320 align:middle
There are other ways to access
the compiled methods, as shown here.

00:07:46.680 --> 00:07:49.080 align:middle
Behavior is the essence of a class.

00:07:50.280 --> 00:07:52.040 align:middle
What is ClassDescription?

00:07:52.400 --> 00:07:56.600 align:middle
It's an abstract superclass
shared by Class and Metaclass.

00:07:56.800 --> 00:07:59.800 align:middle
It adds facilities to Behavior,

00:08:00.000 --> 00:08:04.920 align:middle
such as naming instance variables,
since class execution can be automatic.

00:08:05.080 --> 00:08:07.520 align:middle
ClassDescription adds such things.

00:08:07.720 --> 00:08:13.080 align:middle
Category organization concerns methods
in your browser showing the protocols.

00:08:13.320 --> 00:08:17.680 align:middle
For example, a method can be stored
in the printing protocol.

00:08:18.200 --> 00:08:20.000 align:middle
Also, the notion of a name.

00:08:20.200 --> 00:08:25.840 align:middle
Plus the maintenance of Change sets
and the saving of changes in files.

00:08:26.280 --> 00:08:28.520 align:middle
You can look at that yourself.

00:08:28.760 --> 00:08:34.080 align:middle
This categorization exists because
use is shared by Class and Metaclass.

00:08:35.000 --> 00:08:38.600 align:middle
The responsibility of Class

00:08:38.840 --> 00:08:42.080 align:middle
is to represent the classes
we employ in Pharo.

00:08:42.320 --> 00:08:46.320 align:middle
This includes representation
for classVariable names

00:08:46.600 --> 00:08:50.080 align:middle
and a better way of naming
and compiling elements.

00:08:50.560 --> 00:08:52.680 align:middle
Once again, refer to the code.

00:08:54.400 --> 00:08:57.320 align:middle
Classes are instances of metaclasses.

00:08:57.560 --> 00:09:03.080 align:middle
If Object is an instance
of a class named Object class,

00:09:03.280 --> 00:09:06.400 align:middle
and likewise for OrderedCollection,

00:09:06.680 --> 00:09:11.080 align:middle
the same applies to Class,
ClassDescription, and Behavior.

00:09:11.680 --> 00:09:14.400 align:middle
So, Class is an instance of Class class

00:09:14.600 --> 00:09:19.800 align:middle
and ClassDescription is an instance
of ClassDescription class.

00:09:20.400 --> 00:09:22.360 align:middle
The same applies to Behavior.

00:09:22.560 --> 00:09:25.920 align:middle
We must also look at
the inheritance chain.

00:09:26.160 --> 00:09:30.840 align:middle
If there's an inheritance relationship
between these two elements,

00:09:31.040 --> 00:09:34.440 align:middle
the same relationship exists
here on the right.

00:09:34.720 --> 00:09:38.400 align:middle
As you see here,
we can trace the inheritance chain.

00:09:39.280 --> 00:09:43.760 align:middle
Behavior class therefore inherits
from Object class.

00:09:44.280 --> 00:09:46.400 align:middle
Now for the second last point.

00:09:46.760 --> 00:09:51.400 align:middle
Since every class is an instance
of a metaclass, a question arises.

00:09:52.360 --> 00:09:56.400 align:middle
What is OrderedCollection class
an instance of?

00:09:56.920 --> 00:09:59.600 align:middle
Since everything is an object in Pharo.

00:10:00.400 --> 00:10:04.720 align:middle
OrderedCollection class
is therefore an instance of Metaclass.

00:10:06.200 --> 00:10:10.320 align:middle
Object class and Class class
are instances of Metaclass.

00:10:10.600 --> 00:10:16.080 align:middle
All the metaclasses in the system
are instances of Metaclass.

00:10:17.400 --> 00:10:21.400 align:middle
Furthermore, Metaclass inherits
from ClassDescription.

00:10:21.600 --> 00:10:25.400 align:middle
It's an unusual class
because it only has one instance.

00:10:26.000 --> 00:10:31.200 align:middle
And you can't name it because its name
is defined by its instance.

00:10:31.400 --> 00:10:33.200 align:middle
That may seem barbaric.

00:10:33.640 --> 00:10:36.160 align:middle
So, we see that OrderedCollection

00:10:36.400 --> 00:10:41.080 align:middle
is an instance of OrderedCollection
class, which takes its name.

00:10:41.400 --> 00:10:44.840 align:middle
All metaclasses
are instances of Metaclass.

00:10:45.160 --> 00:10:49.720 align:middle
In the code,
the main responsibility of Metaclass

00:10:50.200 --> 00:10:55.720 align:middle
is to create and initialize
a single instance of itself.

00:10:56.080 --> 00:10:59.920 align:middle
There's only one question
left to answer.

00:11:01.080 --> 00:11:04.400 align:middle
What is Metaclass an instance of?

00:11:04.760 --> 00:11:10.640 align:middle
Because Metaclass is a class,
like OrderedCollection,

00:11:11.000 --> 00:11:14.320 align:middle
it is an instance
of the class Metaclass class.

00:11:14.920 --> 00:11:20.280 align:middle
You'll see that Metaclass
inherits from ClassDescription,

00:11:21.200 --> 00:11:26.080 align:middle
therefore Metaclass class
inherits from ClassDescription class.

00:11:26.840 --> 00:11:30.840 align:middle
OrderedCollection class
inherits from Object class

00:11:31.000 --> 00:11:33.920 align:middle
as OrderedCollection
inherits from Object.

00:11:34.080 --> 00:11:38.840 align:middle
So, Metaclass is an instance
of Metaclass class.

00:11:39.280 --> 00:11:43.360 align:middle
OrderedCollection is an instance
of OrderedCollection class.

00:11:43.640 --> 00:11:48.280 align:middle
And Metaclass class
is an instance of Metaclass,

00:11:48.440 --> 00:11:55.200 align:middle
just as Class class and Object class
are instances of Metaclass.

00:11:55.640 --> 00:12:00.640 align:middle
This loop may seem bizarre,
but it makes sense in context.

00:12:00.920 --> 00:12:06.400 align:middle
You don't need this to use Pharo,
so feel free to overlook it.

00:12:06.760 --> 00:12:08.680 align:middle
But as this graph shows,

00:12:08.840 --> 00:12:12.200 align:middle
the key we spoke about earlier
works perfectly.

00:12:12.400 --> 00:12:16.680 align:middle
Every time we send a message
to an object, we look at its class

00:12:16.840 --> 00:12:20.400 align:middle
and its inheritance tree,
as shown in these examples.

00:12:20.760 --> 00:12:25.760 align:middle
If I send a message to this instance,
it leads me to Object.

00:12:26.760 --> 00:12:29.920 align:middle
If I send a message to the class,

00:12:30.840 --> 00:12:34.400 align:middle
I go to the metaclass
and follow the inheritance.

00:12:36.200 --> 00:12:40.600 align:middle
If I send a message to a metaclass,
where should I look?

00:12:41.560 --> 00:12:47.160 align:middle
In Metaclass, since the metaclass
is an instance of Metaclass.

00:12:47.560 --> 00:12:50.920 align:middle
I follow the instantiation link
to the end.

00:12:51.600 --> 00:12:57.080 align:middle
This is when a message is not understood
or a method is only defined in Object.

00:12:57.600 --> 00:13:02.600 align:middle
Now, what if I send a message
to Metaclass itself?

00:13:02.840 --> 00:13:05.080 align:middle
Where do I search?

00:13:05.400 --> 00:13:10.200 align:middle
First in Metaclass class,
then I follow the inheritance chain.

00:13:11.400 --> 00:13:13.400 align:middle
So... oops!

00:13:13.920 --> 00:13:17.520 align:middle
It gets more complicated here.

00:13:19.520 --> 00:13:23.680 align:middle
Similarly, if I send a message
to Metaclass class,

00:13:23.840 --> 00:13:26.520 align:middle
or to OrderedCollection class,

00:13:26.840 --> 00:13:29.160 align:middle
I look in the instance

00:13:30.080 --> 00:13:33.040 align:middle
of Metaclass class, which is Metaclass.

00:13:33.200 --> 00:13:36.920 align:middle
Like for OrderedCollection class,
I follow this path.

00:13:37.200 --> 00:13:40.840 align:middle
You will see
that this graph is entirely logical.

00:13:41.560 --> 00:13:44.920 align:middle
We cannot have an inconsistent graph

00:13:45.360 --> 00:13:48.520 align:middle
since the virtual machine
only does one thing.

00:13:48.680 --> 00:13:53.400 align:middle
It looks for messages in the class
and follows the inheritance chain.

00:13:53.560 --> 00:13:58.360 align:middle
This comprehensive graph
is consistent with that process.

00:13:59.040 --> 00:14:03.520 align:middle
I find this exciting because
I wondered about it myself.

00:14:03.840 --> 00:14:08.280 align:middle
So, to sum up, classes are objects
and can receive messages.

00:14:08.600 --> 00:14:12.200 align:middle
The procedure is the same
for all objects,

00:14:12.400 --> 00:14:16.840 align:middle
even if there are metaclasses
with a sole instance,

00:14:17.040 --> 00:14:19.080 align:middle
which are not explicitly named.

00:14:19.400 --> 00:14:22.080 align:middle
But it's not essential to know that.

