X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/sod/blobdiff_plain/4b8e5c0347115ff30841f1d1e71afe59ecb6c82c..23e44cba8fe33e8cdde22d1ae14d78d992b9f8bb:/src/utilities.lisp diff --git a/src/utilities.lisp b/src/utilities.lisp index 099c4ba..98d314a 100644 --- a/src/utilities.lisp +++ b/src/utilities.lisp @@ -713,6 +713,18 @@ (defun ,from (object) `((defun (setf ,from) (value object) (setf (,to object) value)))))) +(export 'define-on-demand-slot) +(defmacro define-on-demand-slot (class slot (instance) &body body) + "Defines a slot which computes its initial value on demand. + + Sets up the named SLOT of CLASS to establish its value as the implicit + progn BODY, by defining an appropriate method on `slot-unbound'." + (with-gensyms (classvar slotvar) + `(defmethod slot-unbound + (,classvar (,instance ,class) (,slotvar (eql ',slot))) + (declare (ignore ,classvar)) + (setf (slot-value ,instance ',slot) (progn ,@body))))) + ;;;-------------------------------------------------------------------------- ;;; CLOS hacking.