3 First metaclasses

In this chapter, we will study how all the concepts explained in the previous chapter fit together to let us define powerful metaclasses. Studying such entities will reinforce your understanding of the instantiation and inheritance relationships as well as their interplay. At the end of the chapter, we will explain why the sixth predicate of ObjVLisp is wrong and propose a solution that elegantly fits the model.

3.1 Defining a new Metaclass

Now we can study how we can add new metaclasses and see how the system handles them. To create a new metaclass is simple; it is enough to inherit from an existing one. Maybe this is obvious to you, but this is what we will check now.

Abstract metaclass: its instance (i.e., the class Node) is abstract.
Figure 3.1 Abstract metaclass: its instance (i.e., the class Node) is abstract.

Abstract

Imagine that we want to define abstract classes. We state that a class is abstract if it cannot create instances. To control the creation of instances of a class, we should define a new metaclass that forbids it. Therefore we will define a metaclass whose instances (abstract classes) cannot create instances.

We create a new metaclass named AbstractMetaclass which inherits from Class and we redefine the method new in this metaclass to raise an error (as shown in Figure 3.1). The following code snippet defines this new metaclass.

Class new
	:name 'AbstractMetaclass'
	:superclass 'Class'
AbstractMetaclass
	addMethod: #new
	body: [ :receiver :initargs | receiver error: 'Cannot create instance of class' ]

Two facts describe the relations between this metaclass and the class Class:

Abstract metaclass at work: Using message passing as a key to navigate the graph.
Figure 3.2 Abstract metaclass at work: Using message passing as a key to navigate the graph.

Now we can define an abstract class Node in ObjVLisp syntax:

AbstractMetaclass new 
	:name 'Node' 
	:super 'Object'

Sending a message new to the class Node will raise an error.

Node new
>>> Cannot create instance of class

A subclass of Node, for example, Workstation, can be a concrete class by being an instance of Class instead of AbstractMetaclass but still inheriting from Node. What we see in Figure 3.2 is that there are two links, instantiation and inheritance. The method lookup follows them as we presented previously. It always starts in the class of the receiver and follows the inheritance path.

What is key to understand is that when we send the message new to the class Workstation, we look for methods first in the metaclass Class. When we send the message new to class Node, we look in its class: AbstractMetaclass as shown in Figure 3.2. In fact we do what we do for any instances: we look at the class of the receiver.

A class method is just implemented and follows the same semantics as instance methods: Sending the message error to the class Node starts in AbstractMetaclass. Since we did not redefine it locally and it is not found there, the lookup will continue in the superclass of AbstractClass: the class Class and then the superclass of class Class, the class Object.

3.2 About class state

Imagine that we define a metaclass WithSingleton whose instances are classes that will have a unique instance. The situation is described in Figure 3.3. The class WithSingleton inherits from Class since it wants to reuse all of the class mechanisms. It is also an instance of class Class, since WithSingleton is a class and it can create instances. The class Node is an instance of class WithSingleton. When it receives the message new, the method new defined in the class WithSingleton is executed. If the unique instance variable is nil, it invokes the behavior defined in Class and stores it in the unique instance variable, and returns it.

A WithSingleton metaclass: its instances can only have one instance.
Figure 3.3 A WithSingleton metaclass: its instances can only have one instance.

Several questions still should be asked and answered.

Instance variable of WithSingleton

As with any class, a subclass gets its instance variables as well as the instance variables of its superclass. Hence WithSingleton instance variables are the same as the one of Class, and it also has unique instance variable as shown in Fig. 3.4.

Singleton objIVs
>>> #(class name superclass iv keywords methodDict unique)

Storing unique instance.
Figure 3.4 Storing unique instance.

Where is the singleton stored?

Each class instance of WithSingleton will have an additional value after its method dictionary. This is where the actual singleton of the class is stored. The class Node and Process are instances of the metaclass WithSingleton. Therefore they have one extra field in their structure to hold the unique instance variable values. Each instance of WithSingleton will have its own value: the instance of class playing the singleton role.

3.3 About the 6th postulate

As mentioned at the start of this chapter, the 6th postulate of ObjVLisp is wrong. Let us read it again:

"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 a 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]."

This says that class instance variables are equivalent to shared variables between instances, and this is wrong. Let us study this. According to the 6th postulate, a shared variable between instances is equal to an instance variable of the class. The definition is not totally clear so let us look at an example given in the article.

Illustrating the problem

Imagine that we would like the constant character '*' to be a class variable shared by all the points of the same class. We redefine the Point class as before, but metaclass of which (let us call it MetaPoint) specifies this common character For example, if a point has a shared variable named char, this instance variable should be defined in the class of the class Point called MetaPoint. The author proposes to define a new metaclass MetaPoint to hold a new instance variable to represent a shared variable between points.

Class new
	:name 'MetaPoint'
	:superclass 'Class'
	:iv #(char)

Then he proposes to use it as follows:

MetaPoint new
	:name Point
	:superclass 'Object'
	:iv #(x y)
	:char '*'

The class Point can define a method that accesses the character just by going to the class level. So why is this approach is wrong? Because it mixes levels. The instance variable char is not class information. It describes the terminal instances and not the instance of the metaclass. Why would the metaclass MetaPoint need a char instance variable?

The solution

The solution is that the shared variable char should be held in a list of the shared variables of the class Point. Any point instance can access this variable. The implication is that a class should have extra information to describe it. That is, an instance variable sharedVariable holding pairs, i.e., variable and its value. We should then be able to write:

Class new
	:name Point
	:superclass 'Object'
	:iv #(x y)
	:sharedivs {#char -> '*'}

Therefore the metaclass Class should get an extra instance variable named sharedivs, and each of its instances (the classes Point, Node, Object) can have different values. Such values can be shared among their instances by the compiler.

What we see is that sharedivs is from the Class vocabulary and we do not need an extra metaclass each time we want to share a variable. This design is similar to the one of Pharo where a class has a classVariable instance variable holding variables shared in all the subclasses of the class defining it.

3.4 Conclusion

We presented a small kernel composed of two classes: Object, the root of the inheritance tree, and Class, the first metaclass root of the instantiation tree. We revisited all the key points related to method lookup, object and class creation, and initialization. In the subsequent chapter, we propose to you how to implement such a kernel.

Further readings

The kernel presented in this chapter is a kernel with explicit metaclasses and as such it is not a panacea. Indeed it results in problems with metaclass composition as explained in Bouraqadi et al.'s excellent article.