summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/subr-x.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2021-10-01 14:33:37 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2021-10-01 14:33:37 -0400
commit3c972723e44c9428ea990562033acfbd84ed29d9 (patch)
treea35464bd2e6756ce6bda2efc3cf5d47568eaec3a /lisp/emacs-lisp/subr-x.el
parent99884c2264715b3ff811320a859d644db08ea90e (diff)
downloademacs-3c972723e44c9428ea990562033acfbd84ed29d9.tar.gz
* lisp/emacs-lisp/subr-x.el (with-memoization): New macro
Extracted from `cl-generic.el`. * lisp/emacs-lisp/cl-generic.el (cl--generic-get-dispatcher) (cl--generic-build-combined-method, cl-generic-generalizers): Use it. (cl--generic-with-memoization): Delete.
Diffstat (limited to 'lisp/emacs-lisp/subr-x.el')
-rw-r--r--lisp/emacs-lisp/subr-x.el12
1 files changed, 12 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 3de666682fa..91ebbf9fb92 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -400,6 +400,18 @@ as the new values of the bound variables in the recursive invocation."
(cl-labels ((,name ,fargs . ,body)) #',name)
. ,aargs)))
+(defmacro with-memoization (place &rest code)
+ "Return the value of CODE and stash it in PLACE.
+If PLACE's value is non-nil, then don't bother evaluating CODE
+and return the value found in PLACE instead."
+ (declare (indent 1) (debug (gv-place body)))
+ (gv-letplace (getter setter) place
+ `(or ,getter
+ ,(macroexp-let2 nil val (macroexp-progn code)
+ `(progn
+ ,(funcall setter val)
+ ,val)))))
+
(provide 'subr-x)