Lazy slots

The slot-unbound generic function is useful for initializing slot values on demand.

(defmethod slot-unbound (class (object my-object) 
                         (slot-name (eql 'my-lazy-slot)))
  (setf (slot-value object 'my-lazy-slot)
        (expensive-computation object)))

This method computes the value and initializes the slot’s value. The return value of setf becomes the return value of the initial call that triggered the slot-unbound. Since the slot is no longer unbound, subsequent access to the slot will return the saved value without doing the expensive computation.

The slot can later be cleared with slot-makunbound if it needs to be reinitialized on demand.