Everything is an object, that is, it can receive messages and respond to them.Classes also fall under this “all”. For example, we spawn objects by sending messages to the class whose instance we want to create:
point := Point x: 1 y: 2.
No supernatural special operations for the miraculous creation of objects, only the sending of messages. In this case, we send the message #x:y:
class Point
in the hope of getting a point in two coordinates.The behavior of an object is determined by its class: the class defines a set of methods that describe the reaction of instances to a particular message.So, all methods for points are defined in the class
Point
- this is understandable. We can recognize the class of an object by sending it a #class
message. point class."-> Point"
But where is the #x:y:
method, which describes the processing of the same-name message by the class Point
defined?#x:y:
must be defined in the class instance of which is the class Point
. The construction “class of a class” is replaced by the term “metaclass”. Of course, the Point
metaclass can be obtained by sending the #class
message #class
the Point
class: point class class."-> Point class" Point class."-> Point class"
As you can see, the Point
metaclass is not assigned its own name in the system. Metaclasses are denoted by smalltalk expressions with which they can be obtained.#x:y:
in this case could be sent to any class: String
, Integer
, etc. Obviously, this is not very good. The alternative is the complete absence of custom behavior on the side of the class (which is no better), or (even worse) black magic at the level of the language, depending on otherworldly forces, on the level of the language - like in most "modern" mainstream creations.Metaclass
: Point class class."-> Metaclass"
Based on the same principles, and applying the same logic, we conclude that Metaclass
should also have a metaclass: Metaclass class."-> Metaclass class"
Please note that metaclass Metaclass
did not receive its own name (as unnecessary).Metaclass
: Metaclass class class."-> Metaclass"
Point superclass."-> Object" Point class superclass."-> Object class" Object superclass."-> ProtoObject" Object superclass class."-> ProtoObject class"
This rule (forced) is violated only where the inheritance chain terminates: ProtoObject does not have a superclass, but its metaclass must be a class: ProtoObject superclass."-> nil" ProtoObject class superclass."-> Class"
Metaclass superclass."-> ClassDescription"
And for those who want to complete the picture completely, but do not want to run Smalltalk: Class superclass."-> ClassDescription" ClassDescription superclass."-> Behavior" Behavior superclass."-> Object"
Source: https://habr.com/ru/post/207982/
All Articles