Node:Changing the Class of an Instance, Next:, Previous:Redefining a Class, Up:Reference Manual



3.9 Changing the Class of an Instance

You can change the class of an existing instance by invoking the generic function change-class with two arguments: the instance and the new class.

change-class generic

The default method for change-class decides how to implement the change of class by looking at the slot definitions for the instance's existing class and for the new class. If the new class has slots with the same name as slots in the existing class, the values for those slots are preserved. Slots that are present only in the existing class are discarded. Slots that are present only in the new class are initialized using the corresponding slot definition's init function (see slot-init-function).

change-class (obj <object>) (new <class>) method
Modify instance obj to make it an instance of class new.

The value of each of obj's slots is preserved only if a similarly named slot exists in new; any other slot values are discarded.

The slots in new that do not correspond to any of obj's pre-existing slots are initialized according to new's slot definitions' init functions.

Customized change of class behaviour can be implemented by defining change-class methods that are specialized either by the class of the instances to be modified or by the metaclass of the new class.

When a class is redefined (see Redefining a Class), and the default class redefinition behaviour is not overridden, GOOPS (eventually) invokes the change-class generic function for each existing instance of the redefined class.