Skip to main content

slot-makunbound

slot-makunbound Function

Syntax:

slot-makunbound instance slot-name → instance

Arguments and Values:

instance – instance.

Slot-name—a symbol.

Description:

The function slot-makunbound restores a slot of the name slot-name in an instance to the unbound state.

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-makunbound)

(Any values returned by slot-missing in this case are ignored by slot-makunbound.)

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-boundp, slot-missing

Notes:

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

slot-missing

Expanded Reference: slot-makunbound

Making a Slot Unbound

slot-makunbound restores a slot to the unbound state. After calling it, slot-boundp returns nil for that slot, and slot-value will signal an error (or invoke slot-unbound).

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

(let ((b (make-instance 'box :content "treasure")))
(format t "Bound: ~A~%" (slot-boundp b 'content))
(slot-makunbound b 'content)
(slot-boundp b 'content))
.. Bound: T
..
=> NIL

Resetting Slots

slot-makunbound can be used to "clear" a slot, forcing it to behave as if it was never initialized.

(defclass cache-entry ()
((key :initarg :key :accessor entry-key)
(value :initarg :value :accessor entry-value)))

(defun clear-cache-value (entry)
(slot-makunbound entry 'value)
entry)

(let ((e (make-instance 'cache-entry :key :x :value 42)))
(clear-cache-value e)
(slot-boundp e 'value))
=> NIL

Returns the Instance

slot-makunbound returns the instance itself, which allows chaining.

(defclass pair ()
((a :initarg :a)
(b :initarg :b)))

(let ((p (make-instance 'pair :a 1 :b 2)))
(eq p (slot-makunbound p 'a)))
=> T