Skip to main content

make-load-form-saving-slots

make-load-form-saving-slots Function

Syntax:

make-load-form-saving-slots object &key slot-names environment

→ creation-form, initialization-form

Arguments and Values:

object—an object.

slot-names—a list.

environment—an environment object.

creation-form—a form.

initialization-form—a form.

Description:

Returns forms that, when evaluated, will construct an object equivalent to object, without executing initialization forms. The slots in the new object that correspond to initialized slots in object are initialized using the values from object. Uninitialized slots in object are not initialized in the new object. make-load-form-saving-slots works for any instance of standard-object or structure-object.

Slot-names is a list of the names of the slots to preserve. If slot-names is not supplied, its value is all of the local slots.

make-load-form-saving-slots returns two values, thus it can deal with circular structures. Whether the result is useful in an application depends on whether the object’s type and slot contents fully capture the application’s idea of the object’s state.

Environment is the environment in which the forms will be processed.

See Also:

make-load-form, make-instance, setf, slot-value, slot-makunbound

Notes:

make-load-form-saving-slots can be useful in user-written make-load-form methods.

When the object is an instance of standard-object, make-load-form-saving-slots could return a creation form that calls allocate-instance and an initialization form that contains calls to setf of slot-value and slot-makunbound, though other functions of similar effect might actually be used.

Expanded Reference: make-load-form-saving-slots

Basic Usage

make-load-form-saving-slots is a helper function that generates creation and initialization forms to reconstruct an object by saving and restoring its slot values. It is the simplest way to implement make-load-form.

(defclass settings ()
((theme :initarg :theme :accessor settings-theme)
(volume :initarg :volume :accessor settings-volume)))

(defmethod make-load-form ((s settings) &optional environment)
(make-load-form-saving-slots s :environment environment))

(let ((s (make-instance 'settings :theme :dark :volume 80)))
(multiple-value-bind (creation init) (make-load-form s)
(list (not (null creation)) (not (null init)))))
=> (T T)

Selecting Specific Slots

The :slot-names keyword lets you choose which slots to save. Slots not listed will be left unbound in the reconstructed object.

(defclass record ()
((id :initarg :id :reader record-id)
(data :initarg :data :reader record-data)
(cache :initform nil :accessor record-cache)))

(defmethod make-load-form ((r record) &optional environment)
;; Only save id and data; cache will be recomputed
(make-load-form-saving-slots r
:slot-names '(id data)
:environment environment))

Works with Structures

make-load-form-saving-slots also works with defstruct-defined types.

(defstruct point x y)

(defmethod make-load-form ((p point) &optional environment)
(make-load-form-saving-slots p :environment environment))

Handling Circular Structures

Because make-load-form-saving-slots returns two values (creation and initialization), it can handle circular references between objects, since all objects are allocated before any initialization forms run.

(defclass node ()
((value :initarg :value :accessor node-value)
(next :initarg :next :accessor node-next :initform nil)))

(defmethod make-load-form ((n node) &optional environment)
(make-load-form-saving-slots n :environment environment))