Skip to main content

reinitialize-instance

reinitialize-instance Standard Generic Function

Syntax:

reinitialize-instance instance &rest initargs &key &allow-other-keys → instance

Method Signatures:

reinitialize-instance (instance standard-object) &rest initargs

Arguments and Values:

instance—an object.

initargs—an initialization argument list.

Description:

The generic function reinitialize-instance can be used to change the values of local slots of an instance according to initargs. This generic function can be called by users.

The system-supplied primary method for reinitialize-instance checks the validity of initargs and signals an error if an initarg is supplied that is not declared as valid. The method then calls the generic function shared-initialize with the following arguments: the instance, nil (which means no slots should be initialized according to their initforms), and the initargs it received.

Side Effects:

The generic function reinitialize-instance changes the values of local slots.

Exceptional Situations:

The system-supplied primary method for reinitialize-instance signals an error if an initarg is supplied that is not declared as valid.

See Also:

initialize-instance, shared-initialize, update-instance-for-redefined-class,

update-instance-for-different-class, slot-boundp, slot-makunbound, Section 7.3 (Reinitial izing an Instance), Section 7.1.4 (Rules for Initialization Arguments), Section 7.1.2 (Declaring the Validity of Initialization Arguments)

Notes:

Initargs are declared as valid by using the :initarg option to defclass, or by defining methods for reinitialize-instance or shared-initialize. The keyword name of each keyword parameter specifier in the lambda list of any method defined on reinitialize-instance or shared-initialize is declared as a valid initialization argument name for all classes for which that method is applicable.

Expanded Reference: reinitialize-instance

Changing Slot Values on an Existing Instance

reinitialize-instance updates the slot values of an existing instance using initargs. Only slots whose initargs are supplied are changed; other slots retain their current values.

(defclass config ()
((host :initarg :host :accessor config-host :initform "localhost")
(port :initarg :port :accessor config-port :initform 8080)))

(let ((c (make-instance 'config)))
(reinitialize-instance c :port 3000)
(list (config-host c) (config-port c)))
=> ("localhost" 3000)

Difference from make-instance

Unlike make-instance, reinitialize-instance does not evaluate :initform forms for slots not mentioned in the initargs. It only changes the slots for which initargs are explicitly provided.

(defclass counter ()
((count :initarg :count :accessor counter-count :initform 0)
(label :initarg :label :accessor counter-label :initform "default")))

(let ((c (make-instance 'counter :count 10 :label "hits")))
;; Only update count; label remains unchanged
(reinitialize-instance c :count 0)
(list (counter-count c) (counter-label c)))
=> (0 "hits")

Custom :after Methods for Reinitialization

You can define :after methods to perform additional actions when an instance is reinitialized.

(defclass rectangle ()
((width :initarg :width :accessor rect-width)
(height :initarg :height :accessor rect-height)
(area :accessor rect-area)))

(defmethod initialize-instance :after ((r rectangle) &key)
(setf (rect-area r) (* (rect-width r) (rect-height r))))

(defmethod reinitialize-instance :after ((r rectangle) &key)
(setf (rect-area r) (* (rect-width r) (rect-height r))))

(let ((r (make-instance 'rectangle :width 3 :height 4)))
(format t "Area: ~A~%" (rect-area r))
(reinitialize-instance r :width 5)
(rect-area r))
.. Area: 12
..
=> 20