close
  • person chevron_right

    VALIDATE-SUPERCLASS explained

    Michał "phoe" Herda – Tuesday, 25 December, 2018 - 16:42 edit

#CommonLisp #Lisp #MOP

(This blogpost is also known as "WTF is VALIDATE-SUPERCLASS and why do I have to define methods on it to get a metaclass to work?".)


Let us assume that we have a set of superclasses. In this case, let us define two of them, but the number does not matter: can be zero, can be one, can be five.

Additionally, let us define a new subclass of STANDARD-CLASS, that we will call our metaclass.

(defclass superclass-1 () ())
(defclass superclass-2 () ())
(defclass metaclass (standard-class) ())

Let us attempt to create a subclass of these two superclasses, while also using our newly created metaclass.

(defclass subclass (superclass-1 superclass-2) ()
  (:metaclass metaclass))

This signals an error.

The class #<STANDARD-CLASS SUPERCLASS-1> was specified as a superclass of 
the class #<METACLASS SUBCLASS {100377A0D3}>, but the metaclasses
#<STANDARD-CLASS STANDARD-CLASS> and #<STANDARD-CLASS METACLASS> are
incompatible.

There are four distinct classes mentioned in the error message. First of all, there is one of our superclasses, SUPERCLASS-1, then, there is the class object named SUBCLASS that we are trying to define. Then, there are two more class objects, STANDARD-CLASS and METACLASS.

The magical combination of parentheses that causes the above DEFCLASS form to evaluate successfully is this mysteriously-looking method:

(defmethod validate-superclass
    ((class metaclass) (superclass standard-class))
  t)

Let us analyze why this particular method fixes our problem and allows us to define classes with the metaclass of our choosing.

The MOP description of VALIDATE-SUPERCLASS says that this function is called with two arguments: class, and superclass, to determine whether the class superclass is suitable for use as a superclass of class.

In other words, if Common Lisp Object System is supposed to permit a situation when superclass becomes a superclass of class, this function needs to return true. If such a situation is forbidden, this function must return false.

The MOP says that the default system-defined method on VALIDATE-SUPERCLASS returns true only in three cases:

  1. if the superclass argument is the class named T,
  2. if the class of the class argument is the same as the class of the superclass argument, or
  3. if the class of one of the arguments is STANDARD-CLASS and the class of the other is FUNCALLABLE-STANDARD-CLASS.

The first case is trivial; every class is a subclass of the class T, therefore it would be pointless to return false on

The third case is there to assure that standard objects and funcallable standard objects can interoperate. A subclass of a funcallable class does not have to be funcallable, and the other way around, too: a subclass of a non-funcallable class may be funcallable.

The second case is what allows the basics of CLOS class definitions to work. Whenever we define a new class with DEFCLASS without any superclasses, we define a new instance of STANDARD-CLASS that has STANDARD-OBJECT as its superclass. STANDARD-OBJECT is an instance of STANDARD-CLASS; the newly defined class is an instance of STANDARD-CLASS; therefore, VALIDATE-SUPECLASS returns true. Whenever we define a new class with any number of superclasses that itself are also instances of STANDARD-CLASS, the same thing happens: each of the superclasses is an instance of STANDARD-CLASS, just like the class we are defining right now, which allows VALIDATE-SUPERCLASS to return true in each single case.

The issue happens when we specify the :METACLASS option to DEFCLASS. :METACLASS states that the new class should be an instance of the provided class. If we pass the option (:METACLASS FOO-CLASS), then the newly instantiated class metaobject will be an instance of FOO-CLASS.

But, remember that we also need to specify superclasses for that class! Unless we explicitly specify a superclass, then it is going to default to STANDARD-OBJECT, in which case, VALIDATE-SUPERCLASS will be called on a pair of arguments: the class we are attempting to create, an instance of FOO-CLASS, and the class STANDARD-OBJECT, an instance of STANDARD-CLASS, which is stated to be its superclass.

Neither of the three cases the default method fit this combination, therefore the method returns NIL. The validation fails, and an error is signaled from the DEFCLASS macro.

This is why we explicitly need to state that we, the programmers, want class STANDARD-OBJECT to be a valid superclass for instances of FOO-CLASS. This is the purpose of the VALIDATE-SUPERCLASS method defined on FOO-CLASS as the class we want to instantiate and STANDARD-OBJECT as the superclass we want to assign to the class.

In other words, the way the aforementioned MOP usage works this way is because VALIDATE-SUPERCLASS serves a greater function than mere playing with metaclasses - it is the central MOP mechanism for declaring which combinations of classes and subclasses are allowed in the Lisp image. Methods on that function which may be specialized in any way, including methods that use EQL-specializers and perform arbitrary computation.

For an example of the above, let us state that the class named FOO may be subclassed only by classes whose name contain the letter E.

(defmethod sb-mop:validate-superclass
    ((class class) (superclass (eql (find-class 'foo))))
  (find-if (lambda (x) (member x '(#\E #\e)))
           (symbol-name (class-name class))))

The above snippet denies (defclass bar (foo) ()), but permits (defclass fred (foo) ()).

It is also possible to imagine a class hierarchy where a programmer decides, for any reason, that a class named BAR should not be allowed to be subclassed at all. The following snippet is enough to achieve that:

(defmethod sb-mop:validate-superclass
    ((class class) (superclass (eql (find-class 'bar))))
  nil)

(To add to the confusion, the error message we get from SBCL in such a case is not very helpful, as it states, the metaclasses #1=#<STANDARD-CLASS COMMON-LISP:STANDARD-CLASS> and #1# are incompatible. I have raised a bugticket on that edge case.)


Summing up, VALIDATE-SUPERCLASS is a general mechanism for describing which kinds of superclass-subclass relations we want to see in the Lisp image. Practice has shown that this function is particularly often used in context of permitting instances of non-STANDARD-CLASSes to inherit from instances of STANDARD-CLASSes - in other words, allowing metaclasses to be instantiated. The way it is done is via defining methods on this function, specialized on the metaclass we want to instantiate, and the superclass that we want to assign to the instances of that metaclass - either a particular superclass of our choosing, or STANDARD-OBJECT as the default. These methods must return true whenever we want to permit a particular combination to happen.