1 00:00:00,480 --> 00:00:03,880 Hello, this is a very advanced course. 2 00:00:04,720 --> 00:00:08,040 We'll focus on how Pharo organizes classes 3 00:00:08,760 --> 00:00:11,840 and instance relationships between classes. 4 00:00:12,400 --> 00:00:15,720 It's really aimed at enthusiasts. 5 00:00:16,520 --> 00:00:21,600 It's not essential to master, since you already use Pharo without it. 6 00:00:22,000 --> 00:00:26,200 You can use Pharo for programming without knowing how it works. 7 00:00:26,400 --> 00:00:31,840 But you may be unsatisfied if the course didn't explain how it functions. 8 00:00:32,000 --> 00:00:34,640 Like explaining where "new" is defined, 9 00:00:34,800 --> 00:00:37,400 or what the class of a metaclass is. 10 00:00:37,920 --> 00:00:40,840 This video will explain all this stuff. 11 00:00:41,040 --> 00:00:45,320 It doesn't matter if you don't understand straightaway. 12 00:00:45,400 --> 00:00:47,920 But if it really interests you, 13 00:00:48,680 --> 00:00:50,840 you can watch it several times. 14 00:00:51,720 --> 00:00:54,320 There is one fundamental key here. 15 00:00:54,760 --> 00:00:59,400 It's a fun aspect, which this transparency should explain. 16 00:00:59,680 --> 00:01:00,680 The basic idea, 17 00:01:00,840 --> 00:01:04,080 which we keep reiterating in this MOOC, 18 00:01:04,200 --> 00:01:09,360 is that, as you should know, when we send a message to an object, 19 00:01:09,640 --> 00:01:13,560 we search in the object's class via the instantiation link. 20 00:01:13,760 --> 00:01:17,200 If it isn't there, we follow the inheritance chain. 21 00:01:17,400 --> 00:01:21,920 We always start in the class, then follow the inheritance chain. 22 00:01:22,400 --> 00:01:26,520 This rule must be followed systematically. 23 00:01:26,920 --> 00:01:29,400 To explain metaclasses, 24 00:01:29,600 --> 00:01:34,840 we summarize them in seven points, which are relatively simple. 25 00:01:35,760 --> 00:01:38,600 Every object is an instance of a class. 26 00:01:40,160 --> 00:01:43,080 That's difficult! Look at this example. 27 00:01:43,320 --> 00:01:47,200 The object OrderedCollection is an instance of this class. 28 00:01:47,600 --> 00:01:49,160 So far, it's clear. 29 00:01:50,080 --> 00:01:55,200 The second point is that every class inherits from the Object class. 30 00:01:57,080 --> 00:01:58,720 As we see here, 31 00:01:59,080 --> 00:02:03,840 OrderedCollection and upwards inherit ultimately from Object. 32 00:02:04,640 --> 00:02:07,040 Just a note for French speakers, 33 00:02:07,200 --> 00:02:09,640 "eventually" means "ultimately." 34 00:02:10,520 --> 00:02:11,560 It's confusing. 35 00:02:11,800 --> 00:02:12,840 So, 36 00:02:13,400 --> 00:02:18,040 we have our inheritance graph that inevitably leads to Object. 37 00:02:20,080 --> 00:02:25,000 What is the responsibility of Object? It represents object behavior 38 00:02:25,200 --> 00:02:28,560 common to all objects, such as error handling, 39 00:02:28,720 --> 00:02:31,080 inserting a halt, and announcements. 40 00:02:31,680 --> 00:02:34,760 Object is the root of the inheritance tree. 41 00:02:35,360 --> 00:02:38,520 Its key responsibility is minimum behavior. 42 00:02:39,800 --> 00:02:44,200 Returning to our list, every class is an instance of a metaclass. 43 00:02:45,800 --> 00:02:48,520 We explained this in an earlier course. 44 00:02:49,080 --> 00:02:53,520 A class X is the sole instance of the metaclass named X class. 45 00:02:53,680 --> 00:02:56,400 We see that the OrderedCollection class 46 00:02:56,560 --> 00:03:00,040 is an instance of the class OrderedCollection class. 47 00:03:00,560 --> 00:03:05,040 It works the same way for the SequenceableCollection class, 48 00:03:05,560 --> 00:03:08,840 and the Object class with the class Object class. 49 00:03:09,080 --> 00:03:12,560 All classes are instances of another metaclass, 50 00:03:12,840 --> 00:03:16,280 which takes the same name followed by "class." 51 00:03:18,920 --> 00:03:20,640 Metaclasses are created 52 00:03:20,840 --> 00:03:23,400 automatically when we create a class. 53 00:03:23,600 --> 00:03:28,320 We send a "new" message to the metaclass to create the class. 54 00:03:30,080 --> 00:03:31,200 Next point. 55 00:03:31,920 --> 00:03:35,400 Metaclass hierarchy parallels class hierarchy. 56 00:03:36,200 --> 00:03:39,160 Earlier, we explained that 57 00:03:39,920 --> 00:03:43,200 OrderedCollection is an instance of its class, 58 00:03:43,400 --> 00:03:46,400 and likewise for SequenceableCollection. 59 00:03:46,640 --> 00:03:51,080 Now we need to specify the relationship between the two. 60 00:03:51,280 --> 00:03:56,040 Since there's an inheritance relationship here, there's also one here. 61 00:03:56,920 --> 00:03:57,920 In all cases. 62 00:03:58,840 --> 00:04:03,400 That's why we say they are two parallel hierarchies. 63 00:04:04,080 --> 00:04:06,320 When do we use this hierarchy? 64 00:04:06,400 --> 00:04:10,080 If I send the "new" message to OrderedCollection, 65 00:04:10,520 --> 00:04:13,080 where do I search? In the class. 66 00:04:13,360 --> 00:04:18,920 I query in each class whether or not "new" has been defined. 67 00:04:19,200 --> 00:04:23,080 Moving upwards, has it defined "new"? Yes or no. 68 00:04:23,320 --> 00:04:28,400 Here we utilize the key I mentioned at the beginning. 69 00:04:28,840 --> 00:04:34,640 To send a message, start with the class then follow the inheritance chain. 70 00:04:35,360 --> 00:04:37,400 Up to this point, 71 00:04:37,640 --> 00:04:41,400 it may seem clear, but now other questions come up. 72 00:04:41,600 --> 00:04:44,360 What is Object class an instance of? 73 00:04:45,200 --> 00:04:46,400 You might wonder. 74 00:04:46,840 --> 00:04:50,200 And what is the superclass of Object class? 75 00:04:51,000 --> 00:04:54,680 Does it work if we send a message to a metaclass? 76 00:04:56,160 --> 00:04:59,400 Or if we send a message to whatever it is here? 77 00:05:00,000 --> 00:05:01,600 Let's have a look. 78 00:05:02,680 --> 00:05:07,800 The system says that every metaclass inherits from Class, 79 00:05:08,280 --> 00:05:10,080 right up to Behavior. 80 00:05:11,000 --> 00:05:14,680 Let's look at that, and answer the first question. 81 00:05:14,840 --> 00:05:18,800 Object class is a class, hence it inherits from Class. 82 00:05:19,560 --> 00:05:23,320 Class inherits from ClassDescription and Behavior. 83 00:05:24,160 --> 00:05:28,040 Unlike in Pharo, in other systems such as Lisp 84 00:05:28,560 --> 00:05:31,200 these two elements are a single class. 85 00:05:31,680 --> 00:05:33,640 Pharo divides them for reuse. 86 00:05:33,840 --> 00:05:36,920 Metaclass can be attached to ClassDescription 87 00:05:37,080 --> 00:05:39,920 to utilize ClassDescription in two cases. 88 00:05:40,080 --> 00:05:44,400 We see that Behavior, which represents the essence of a class, 89 00:05:44,600 --> 00:05:46,720 inherits from Object. 90 00:05:49,920 --> 00:05:53,000 So, if I send a message here, 91 00:05:53,360 --> 00:05:56,040 it will travel all the way up to here. 92 00:05:57,280 --> 00:06:00,840 It follows the inheritance tree from the Class level. 93 00:06:03,080 --> 00:06:06,200 Now we want to know where "new" is defined. 94 00:06:06,840 --> 00:06:10,840 The method for creating instances is defined in Behavior. 95 00:06:11,920 --> 00:06:17,680 When I send the "new" message to OrderedCollection, what happens? 96 00:06:18,080 --> 00:06:20,400 First, I search in the class. 97 00:06:21,080 --> 00:06:24,920 Suppose that "new" is not redefined in the inheritance, 98 00:06:25,080 --> 00:06:28,000 so I search all of the superclasses 99 00:06:28,360 --> 00:06:29,760 until we find "new." 100 00:06:29,920 --> 00:06:34,600 In a lookup, we search for a method and execute it on the receiver. 101 00:06:34,840 --> 00:06:39,000 So, I find the "new" method and execute it on the receiver, 102 00:06:39,200 --> 00:06:44,200 the class OrderedCollection, which creates a new instance 103 00:06:44,360 --> 00:06:46,840 that we'll name 3 4, for example. 104 00:06:47,720 --> 00:06:48,720 Okay? 105 00:06:49,400 --> 00:06:52,320 We're using the key I spoke about. 106 00:06:52,560 --> 00:06:57,840 We search the inheritance chain, starting with the receiver's class. 107 00:06:58,840 --> 00:07:01,920 In a nutshell, what does Behavior represent? 108 00:07:02,160 --> 00:07:05,840 It's the essence of an object that can have instances. 109 00:07:06,800 --> 00:07:08,520 Objects with instances 110 00:07:09,760 --> 00:07:12,760 must include a superclass link, 111 00:07:13,000 --> 00:07:16,920 method dictionary, and description of instances (format). 112 00:07:17,080 --> 00:07:22,840 The methods shown here include examples such as new, basicNew and new:. 113 00:07:23,400 --> 00:07:26,520 The difference between new and basicNew 114 00:07:26,680 --> 00:07:29,800 is that you must never redefine basicNew. 115 00:07:30,080 --> 00:07:33,000 As we explained in the course on methods, 116 00:07:33,200 --> 00:07:38,760 if you redefine a method starting with "basic," the method is inaccessible. 117 00:07:39,600 --> 00:07:40,920 The original method. 118 00:07:41,080 --> 00:07:46,320 There are other ways to access the compiled methods, as shown here. 119 00:07:46,680 --> 00:07:49,080 Behavior is the essence of a class. 120 00:07:50,280 --> 00:07:52,040 What is ClassDescription? 121 00:07:52,400 --> 00:07:56,600 It's an abstract superclass shared by Class and Metaclass. 122 00:07:56,800 --> 00:07:59,800 It adds facilities to Behavior, 123 00:08:00,000 --> 00:08:04,920 such as naming instance variables, since class execution can be automatic. 124 00:08:05,080 --> 00:08:07,520 ClassDescription adds such things. 125 00:08:07,720 --> 00:08:13,080 Category organization concerns methods in your browser showing the protocols. 126 00:08:13,320 --> 00:08:17,680 For example, a method can be stored in the printing protocol. 127 00:08:18,200 --> 00:08:20,000 Also, the notion of a name. 128 00:08:20,200 --> 00:08:25,840 Plus the maintenance of Change sets and the saving of changes in files. 129 00:08:26,280 --> 00:08:28,520 You can look at that yourself. 130 00:08:28,760 --> 00:08:34,080 This categorization exists because use is shared by Class and Metaclass. 131 00:08:35,000 --> 00:08:38,600 The responsibility of Class 132 00:08:38,840 --> 00:08:42,080 is to represent the classes we employ in Pharo. 133 00:08:42,320 --> 00:08:46,320 This includes representation for classVariable names 134 00:08:46,600 --> 00:08:50,080 and a better way of naming and compiling elements. 135 00:08:50,560 --> 00:08:52,680 Once again, refer to the code. 136 00:08:54,400 --> 00:08:57,320 Classes are instances of metaclasses. 137 00:08:57,560 --> 00:09:03,080 If Object is an instance of a class named Object class, 138 00:09:03,280 --> 00:09:06,400 and likewise for OrderedCollection, 139 00:09:06,680 --> 00:09:11,080 the same applies to Class, ClassDescription, and Behavior. 140 00:09:11,680 --> 00:09:14,400 So, Class is an instance of Class class 141 00:09:14,600 --> 00:09:19,800 and ClassDescription is an instance of ClassDescription class. 142 00:09:20,400 --> 00:09:22,360 The same applies to Behavior. 143 00:09:22,560 --> 00:09:25,920 We must also look at the inheritance chain. 144 00:09:26,160 --> 00:09:30,840 If there's an inheritance relationship between these two elements, 145 00:09:31,040 --> 00:09:34,440 the same relationship exists here on the right. 146 00:09:34,720 --> 00:09:38,400 As you see here, we can trace the inheritance chain. 147 00:09:39,280 --> 00:09:43,760 Behavior class therefore inherits from Object class. 148 00:09:44,280 --> 00:09:46,400 Now for the second last point. 149 00:09:46,760 --> 00:09:51,400 Since every class is an instance of a metaclass, a question arises. 150 00:09:52,360 --> 00:09:56,400 What is OrderedCollection class an instance of? 151 00:09:56,920 --> 00:09:59,600 Since everything is an object in Pharo. 152 00:10:00,400 --> 00:10:04,720 OrderedCollection class is therefore an instance of Metaclass. 153 00:10:06,200 --> 00:10:10,320 Object class and Class class are instances of Metaclass. 154 00:10:10,600 --> 00:10:16,080 All the metaclasses in the system are instances of Metaclass. 155 00:10:17,400 --> 00:10:21,400 Furthermore, Metaclass inherits from ClassDescription. 156 00:10:21,600 --> 00:10:25,400 It's an unusual class because it only has one instance. 157 00:10:26,000 --> 00:10:31,200 And you can't name it because its name is defined by its instance. 158 00:10:31,400 --> 00:10:33,200 That may seem barbaric. 159 00:10:33,640 --> 00:10:36,160 So, we see that OrderedCollection 160 00:10:36,400 --> 00:10:41,080 is an instance of OrderedCollection class, which takes its name. 161 00:10:41,400 --> 00:10:44,840 All metaclasses are instances of Metaclass. 162 00:10:45,160 --> 00:10:49,720 In the code, the main responsibility of Metaclass 163 00:10:50,200 --> 00:10:55,720 is to create and initialize a single instance of itself. 164 00:10:56,080 --> 00:10:59,920 There's only one question left to answer. 165 00:11:01,080 --> 00:11:04,400 What is Metaclass an instance of? 166 00:11:04,760 --> 00:11:10,640 Because Metaclass is a class, like OrderedCollection, 167 00:11:11,000 --> 00:11:14,320 it is an instance of the class Metaclass class. 168 00:11:14,920 --> 00:11:20,280 You'll see that Metaclass inherits from ClassDescription, 169 00:11:21,200 --> 00:11:26,080 therefore Metaclass class inherits from ClassDescription class. 170 00:11:26,840 --> 00:11:30,840 OrderedCollection class inherits from Object class 171 00:11:31,000 --> 00:11:33,920 as OrderedCollection inherits from Object. 172 00:11:34,080 --> 00:11:38,840 So, Metaclass is an instance of Metaclass class. 173 00:11:39,280 --> 00:11:43,360 OrderedCollection is an instance of OrderedCollection class. 174 00:11:43,640 --> 00:11:48,280 And Metaclass class is an instance of Metaclass, 175 00:11:48,440 --> 00:11:55,200 just as Class class and Object class are instances of Metaclass. 176 00:11:55,640 --> 00:12:00,640 This loop may seem bizarre, but it makes sense in context. 177 00:12:00,920 --> 00:12:06,400 You don't need this to use Pharo, so feel free to overlook it. 178 00:12:06,760 --> 00:12:08,680 But as this graph shows, 179 00:12:08,840 --> 00:12:12,200 the key we spoke about earlier works perfectly. 180 00:12:12,400 --> 00:12:16,680 Every time we send a message to an object, we look at its class 181 00:12:16,840 --> 00:12:20,400 and its inheritance tree, as shown in these examples. 182 00:12:20,760 --> 00:12:25,760 If I send a message to this instance, it leads me to Object. 183 00:12:26,760 --> 00:12:29,920 If I send a message to the class, 184 00:12:30,840 --> 00:12:34,400 I go to the metaclass and follow the inheritance. 185 00:12:36,200 --> 00:12:40,600 If I send a message to a metaclass, where should I look? 186 00:12:41,560 --> 00:12:47,160 In Metaclass, since the metaclass is an instance of Metaclass. 187 00:12:47,560 --> 00:12:50,920 I follow the instantiation link to the end. 188 00:12:51,600 --> 00:12:57,080 This is when a message is not understood or a method is only defined in Object. 189 00:12:57,600 --> 00:13:02,600 Now, what if I send a message to Metaclass itself? 190 00:13:02,840 --> 00:13:05,080 Where do I search? 191 00:13:05,400 --> 00:13:10,200 First in Metaclass class, then I follow the inheritance chain. 192 00:13:11,400 --> 00:13:13,400 So... oops! 193 00:13:13,920 --> 00:13:17,520 It gets more complicated here. 194 00:13:19,520 --> 00:13:23,680 Similarly, if I send a message to Metaclass class, 195 00:13:23,840 --> 00:13:26,520 or to OrderedCollection class, 196 00:13:26,840 --> 00:13:29,160 I look in the instance 197 00:13:30,080 --> 00:13:33,040 of Metaclass class, which is Metaclass. 198 00:13:33,200 --> 00:13:36,920 Like for OrderedCollection class, I follow this path. 199 00:13:37,200 --> 00:13:40,840 You will see that this graph is entirely logical. 200 00:13:41,560 --> 00:13:44,920 We cannot have an inconsistent graph 201 00:13:45,360 --> 00:13:48,520 since the virtual machine only does one thing. 202 00:13:48,680 --> 00:13:53,400 It looks for messages in the class and follows the inheritance chain. 203 00:13:53,560 --> 00:13:58,360 This comprehensive graph is consistent with that process. 204 00:13:59,040 --> 00:14:03,520 I find this exciting because I wondered about it myself. 205 00:14:03,840 --> 00:14:08,280 So, to sum up, classes are objects and can receive messages. 206 00:14:08,600 --> 00:14:12,200 The procedure is the same for all objects, 207 00:14:12,400 --> 00:14:16,840 even if there are metaclasses with a sole instance, 208 00:14:17,040 --> 00:14:19,080 which are not explicitly named. 209 00:14:19,400 --> 00:14:22,080 But it's not essential to know that.