From 896384b542cabdc000eafb80c9082830f692bbb2 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Thu, 6 May 2021 13:30:52 +0200 Subject: [PATCH] Make (setf (map-elt ...)) return the value in the alist/plist cases * lisp/emacs-lisp/map.el (map-elt): Return the value in the list case (which can signal a `map-not-inplace' error. (map-elt): Return the value in the list case, too (bug#47572). --- lisp/emacs-lisp/map.el | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el index c0cbc7b5a18..5c76fb9eb95 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el @@ -124,7 +124,9 @@ or array." (with-no-warnings (map-put! ,mgetter ,key ,v ,testfn)) (map-not-inplace ,(funcall msetter - `(map-insert ,mgetter ,key ,v)))))))))) + `(map-insert ,mgetter ,key ,v)) + ;; Always return the value. + ,v)))))))) ;; `testfn' is deprecated. (advertised-calling-convention (map key &optional default) "27.1")) ;; Can't use `cl-defmethod' with `advertised-calling-convention'. @@ -429,18 +431,22 @@ To insert an element without modifying MAP, use `map-insert'." ;; `testfn' only exists for backward compatibility with `map-put'! (declare (advertised-calling-convention (map key value) "27.1")) ;; Can't use `cl-defmethod' with `advertised-calling-convention'. - (map--dispatch map - :list - (if (map--plist-p map) - (plist-put map key value) - (let ((oldmap map)) - (setf (alist-get key map key nil (or testfn #'equal)) value) - (unless (eq oldmap map) - (signal 'map-not-inplace (list oldmap))))) - :hash-table (puthash key value map) - ;; FIXME: If `key' is too large, should we signal `map-not-inplace' - ;; and let `map-insert' grow the array? - :array (aset map key value))) + (map--dispatch + map + :list + (progn + (if (map--plist-p map) + (plist-put map key value) + (let ((oldmap map)) + (setf (alist-get key map key nil (or testfn #'equal)) value) + (unless (eq oldmap map) + (signal 'map-not-inplace (list oldmap))))) + ;; Always return the value. + value) + :hash-table (puthash key value map) + ;; FIXME: If `key' is too large, should we signal `map-not-inplace' + ;; and let `map-insert' grow the array? + :array (aset map key value))) (cl-defgeneric map-insert (map key value) "Return a new map like MAP except that it associates KEY with VALUE. -- 2.39.5