Skip to main content

slot-boundp

slot-boundp Function

Syntax:

slot-boundp instance slot-name → generalized-boolean

Arguments and Values:

instance—an object.

slot-name—a symbol naming a slot of instance.

generalized-boolean—a generalized boolean.

Description:

Returns true if the slot named slot-name in instance is bound; otherwise, returns false.

Exceptional Situations:

If no slot of the name slot-name exists in the instance, slot-missing is called as follows:

(slot-missing (class-of instance)

instance

slot-name

’slot-boundp)

(If slot-missing is invoked and returns a value, a boolean equivalent to its primary value is returned by slot-boundp.)

The specific behavior depends on instance’s metaclass. An error is never signaled if instance has metaclass standard-class. An error is always signaled if instance has metaclass built-in-class. The consequences are undefined if instance has any other metaclass–an error might or might not be signaled in this situation. Note in particular that the behavior for conditions and structures is not specified.

See Also:

slot-makunbound, slot-missing

Notes:

The function slot-boundp allows for writing after methods on initialize-instance in order to initialize only those slots that have not already been bound.

Although no implementation is required to do so, implementors are strongly encouraged to implement the function slot-boundp using the function slot-boundp-using-class described in the Metaobject Protocol.

Expanded Reference: slot-boundp

Basic Usage

slot-boundp returns true if the named slot has a value, and false if it is unbound. This is useful for checking whether a slot has been initialized.

(defclass item ()
((name :initarg :name)
(price :initarg :price)))

(let ((i (make-instance 'item :name "Widget")))
(list (slot-boundp i 'name)
(slot-boundp i 'price)))
=> (T NIL)

Checking Before Accessing Unbound Slots

Use slot-boundp to avoid the error that occurs when reading an unbound slot.

(defclass optional-field ()
((value :initarg :value)))

(defun safe-get-value (obj)
(if (slot-boundp obj 'value)
(slot-value obj 'value)
:not-set))

(safe-get-value (make-instance 'optional-field :value 42))
=> 42

(safe-get-value (make-instance 'optional-field))
=> :NOT-SET

Conditional Initialization in :after Methods

slot-boundp is commonly used inside initialize-instance :after methods to set defaults only for slots that were not provided.

(defclass record ()
((id :initarg :id :accessor record-id)
(label :initarg :label :accessor record-label)))

(defmethod initialize-instance :after ((r record) &key)
(unless (slot-boundp r 'label)
(setf (record-label r)
(format nil "Record-~A" (record-id r)))))

(record-label (make-instance 'record :id 1))
=> "Record-1"

(record-label (make-instance 'record :id 1 :label "Custom"))
=> "Custom"

After slot-makunbound

A slot that was once bound can be made unbound again using slot-makunbound. slot-boundp reflects this change.

(defclass container ()
((content :initarg :content :accessor container-content)))

(let ((c (make-instance 'container :content "stuff")))
(list (slot-boundp c 'content)
(progn (slot-makunbound c 'content)
(slot-boundp c 'content))))
=> (T NIL)