Search code examples
common-lispclos

How to "cast" an instance to a subclass?


I have an instance of class message I'll call "msg". I have defined a class "my-message" and would like instance "msg" to now be of that class.

It sounds to me like it should be relatively straightforward, but I don't know how to do it. change-class gives me an error I don't understand.

(defclass my-message (message)
  ((account-name :accessor account-name :initform nil :initarg :account-name)))

(change-class msg 'my-message :account-name account-name)

ERROR :
While computing the class precedence list of the class named MW::MY-MESSAGE.
The class named MW::MESSAGE is a forward referenced class.
The class named MW::MESSAGE is a direct superclass of the class named MW::MY-MESSAGE.

Solution

  • The class named MW::MESSAGE is a forward referenced class.
    

    A forward referenced class is a class that you reference but have not yet defined. If you look at the name of the class, it is MW::MESSAGE. I suppose you want to subclass another class named MESSAGE in another package; there might be something wrong with the symbols you import.

    The class named MW::MESSAGE is a direct superclass of the class named MW::MY-MESSAGE.
    

    Since the MW::MESSAGE class is not yet defined, you cannot make an instance of it. This is also why you cannot make an instance of any of its subclasses, such as MW::MY-MESSAGE.