1 A minimal reflective class-based kernel
The difference between classes and objects has been repeatedly emphasized. In the view presented here, these concepts belong to different worlds: the program text only contains classes; at run-time, only objects exist. This is not the only approach. One of the subcultures of object-oriented programming, influenced by Lisp and exemplified by Smalltalk, views classes as objects themselves, which still have an existence at run-time. — B. Meyer, Object-Oriented Software Construction Meye88b
As this quote expresses it, there is a realm where classes are true objects, instances of other classes. In such systems as Smalltalk, Pharo Blac09a, CLOS Stee90a, classes are described by other classes and form often reflective architectures each one describing the previous level. In this chapter, we will explore a minimal reflective class-based kernel, inspired by ObjVlisp. In the following chapter, you will implement step by step such a kernel with less than 30 methods.
1.1 ObjVlisp inspiration
ObjVlisp was published for the first time in 1987 when the foundation of object-oriented programming was still emerging Coin87a. ObjVlisp has explicit metaclasses and supports metaclass reuse. It was inspired by the kernel of Smalltalk-78. The IBM SOM-DSOM kernel is similar to ObjVLisp while implemented in C++ Form99a. ObjVlisp is a subset of the reflective kernel of CLOS (Common Lisp Object System) since CLOS reifies instance variables, generic functions, and method combinations. It is the equivalent of the Closette implementation Kicz91a. In comparison to ObjVlisp, Smalltalk, and Pharo have implicit metaclasses and no metaclass reuse except by basic inheritance. However, they are more stable as explained by Bouraqadi et al Bour98a.
Studying this kernel is really worth it since it has the following properties:
- It unifies classes and instances (there is only one data structure to represent all objects, classes included).
- It is composed of only two classes
ClassandObject(it relies on existing elements such as booleans, arrays, and strings of the underlying implementation language). - It raises the question of meta-circularity infinite regression (a class is an instance of another class that is an instance of yet another class, etc.) and how to resolve it.
- It requires consideration of allocation, class, and object initialization, message passing as well as the bootstrap process.
- It can be implemented in less than 30 methods in Pharo.
Just remember that this kernel is self-described. We will start to explain some aspects, but since everything is linked, you may have to read the chapter twice to fully get it.
1.2 ObjVLisp's six postulates
The original ObjVlisp kernel is defined by six postulates Coin87a. Some of them look a bit dated by modern standards, and the 6th postulate is simply wrong as we will explain later (a solution is simple to design and implement).
Here are the six postulates as stated in the paper for the sake of historical perspective.
- P1. An object represents a piece of knowledge and a set of capabilities.
- P2. The only protocol to activate an object is message passing: a message specifies which procedure to apply (denoted by its name, the selector) and its arguments.
- P3. Every object belongs to a class that specifies its data (attributes called fields) and its behavior (procedures called methods). Objects will be dynamically generated from this model; they are called instances of the class. Following Plato, all instances of a class have the same structure and shape but differ through the values of their common instance variables.
- P4. A class is also an object, instantiated by another class, called its metaclass. Consequently (P3), to each class is associated with a metaclass which describes its behavior as an object. The initial primitive metaclass is the class
Class, built as its own instance. - P5. A class can be defined as a subclass of one (or many) other class(es). This subclassing mechanism allows the sharing of instance variables and methods and is called inheritance. The class
Objectrepresents the most common behavior shared by all objects. - P6. If the instance variables owned by an object define a local environment, there are also class variables defining a global environment shared by all the instances of the same class. These class variables are defined at the metaclass level according to the following equation: class variable [an-object] = instance variable [an-object’s class].
1.3 Kernel overview
If you do not fully grasp the following overview, don't worry. This full chapter is here to make sure that you will understand it. Let us get started.
Contrary to a real uniform language kernel, ObjVlisp does not consider arrays, booleans, strings, numbers or any other elementary objects as part of the kernel as this is the case in a real bootstrap such as the one of Pharo. ObjVLisp's kernel focuses on understanding Class/Object core relationships.
Figure 1.1 shows the two core classes of the kernel:
Objectwhich is the root of the inheritance graph and is an instance ofClass.Classis the first class and root of the instantiation tree and instance of itself as we will see later.
Figure 1.2 shows that the class Workstation is an instance of the class Class since it is a class and it inherits from Object the default behavior objects should exhibit. The class WithSingleton is an instance of the class Class but in addition, it inherits from Class, since this is a metaclass: its instances are classes. As such, it changes the behavior of classes. The class SpecialWorkstation is an instance of the class WithSingleton and inherits from Workstation, since its instances exhibit the same behavior as Workstation.
The two diagrams 1.1 and 1.2 will be explained step by step throughout this chapter.
The key point of understanding such a reflective architecture is that message passing always looks up methods in the class of the receiver of the message and then follows the inheritance chain (See Figure 1.3).
Figure 1.3 illustrates two main cases:
- When we send a message to
BigMacorMinna, the corresponding method is looked up in their corresponding classesWorkstationorSpecialWorkstationand follows the inheritance link up toObject. - When we send a message to the classes
WorkstationorSpecialWorkstation, the corresponding method is looked up in their class, the classClassand up toObject.
1.4 Conclusion
ObjVLisp is a minimal kernel with its two main classes. It uses the infrastructure of the underlying language (integers, strings, booleans...). When the language is fully reflective such as Pharo all such infrastructure has to be defined at the same level as the class Object and Class but the model principles stay the same.