slot-missing
slot-missing Standard Generic Function
Syntax:
slot-missing class object slot-name operation &optional new-value → {result}*
Method Signatures:
slot-missing (class t) object slot-name
operation &optional new-value
Arguments and Values:
class—the class of object.
object—an object.
slot-name—a symbol (the name of a would-be slot).
operation—one of the symbols setf, slot-boundp, slot-makunbound, or slot-value. new-value—an object.
result—an object.
Description:
The generic function slot-missing is invoked when an attempt is made to access a slot in an object whose metaclass is standard-class and the slot of the name slot-name is not a name of a slot in that class. The default method signals an error.
The generic function slot-missing is not intended to be called by programmers. Programmers may write methods for it.
The generic function slot-missing may be called during evaluation of slot-value, (setf slot-value), slot-boundp, and slot-makunbound. For each of these operations the corresponding symbol for the operation argument is slot-value, setf, slot-boundp, and slot-makunbound respectively.
The optional new-value argument to slot-missing is used when the operation is attempting to set the value of the slot.
If slot-missing returns, its values will be treated as follows:
• If the operation is setf or slot-makunbound, any values will be ignored by the caller.
• If the operation is slot-value, only the primary value will be used by the caller, and all other values will be ignored.
• If the operation is slot-boundp, any boolean equivalent of the primary value of the method might be is used, and all other values will be ignored.
Exceptional Situations:
The default method on slot-missing signals an error of type error.
See Also:
defclass, slot-exists-p, slot-value
Notes:
The set of arguments (including the class of the instance) facilitates defining methods on the metaclass for slot-missing.
Expanded Reference: slot-missing
Default Behavior
slot-missing is called when slot-value, (setf slot-value), slot-boundp, or slot-makunbound is used with a slot name that does not exist on the object. The default method signals an error.
(defclass simple ()
((x :initarg :x)))
(let ((s (make-instance 'simple :x 1)))
(handler-case (slot-value s 'nonexistent)
(error () :caught-error)))
=> :CAUGHT-ERROR
Custom slot-missing for Dynamic Properties
You can define a method on slot-missing to implement a property-list-like fallback for missing slots.
(defclass flexible-object ()
((properties :initform (make-hash-table) :accessor obj-properties)))
(defmethod slot-missing ((class t) (obj flexible-object) slot-name
(operation (eql 'slot-value)) &optional new-value)
(declare (ignore new-value))
(gethash slot-name (obj-properties obj)))
(defmethod slot-missing ((class t) (obj flexible-object) slot-name
(operation (eql 'setf)) &optional new-value)
(setf (gethash slot-name (obj-properties obj)) new-value))
(let ((obj (make-instance 'flexible-object)))
(setf (slot-value obj 'color) :red)
(slot-value obj 'color))
=> :RED
The operation Argument
The operation argument tells you which function triggered the call: slot-value, setf, slot-boundp, or slot-makunbound.
(defclass logged ()
((data :initarg :data)))
(defmethod slot-missing ((class t) (obj logged) slot-name operation
&optional new-value)
(format nil "Missing slot ~A, operation ~A" slot-name operation))
(let ((obj (make-instance 'logged :data 1)))
(slot-value obj 'no-such-slot))
=> "Missing slot NO-SUCH-SLOT, operation SLOT-VALUE"