slot-exists-p
slot-exists-p Function
Syntax:
slot-exists-p object slot-name → generalized-boolean
Arguments and Values:
object—an object.
slot-name—a symbol.
generalized-boolean—a generalized boolean.
Description:
Returns true if the object has a slot named slot-name.
Affected By:
defclass, defstruct
See Also:
defclass, slot-missing
Notes:
Although no implementation is required to do so, implementors are strongly encouraged to implement the function slot-exists-p using the function slot-exists-p-using-class described in the Metaobject Protocol.
Expanded Reference: slot-exists-p
Checking for Slot Existence
slot-exists-p returns true if the object has a slot with the given name, and false otherwise. It does not check whether the slot is bound.
(defclass person ()
((name :initarg :name)
(age :initarg :age)))
(let ((p (make-instance 'person :name "Alice")))
(list (slot-exists-p p 'name)
(slot-exists-p p 'age)
(slot-exists-p p 'address)))
=> (T T NIL)
Guarding Against Missing Slots
slot-exists-p is useful for writing generic code that works with objects from different classes.
(defclass base-record ()
((id :initarg :id :accessor record-id)))
(defclass extended-record (base-record)
((notes :initarg :notes :accessor record-notes)))
(defun safe-get-notes (obj)
(if (slot-exists-p obj 'notes)
(if (slot-boundp obj 'notes)
(slot-value obj 'notes)
"(no notes)")
"(slot not available)"))
(safe-get-notes (make-instance 'extended-record :id 1 :notes "important"))
=> "important"
(safe-get-notes (make-instance 'base-record :id 2))
=> "(slot not available)"
Works with Structures Too
slot-exists-p also works with structure instances.
(defstruct point x y)
(let ((p (make-point :x 1 :y 2)))
(list (slot-exists-p p 'x)
(slot-exists-p p 'z)))
=> (T NIL)