[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: defining a setter function with gv.el
From: |
Stefan Monnier |
Subject: |
Re: defining a setter function with gv.el |
Date: |
Mon, 27 Aug 2012 16:28:32 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.1.50 (gnu/linux) |
>> ;; (defun alist-get (key alist)
>> ;; "Get the value associated to KEY in ALIST."
>> ;; (declare
>> ;; (gv-expander
>> ;; (lambda (do)
>> ;; (macroexp-let2 macroexp-copyable-p k key
>> ;; (gv-letplace (getter setter) alist
>> ;; (macroexp-let2 nil p `(assoc ,k ,getter)
>> ;; (funcall do `(cdr ,p)
>> ;; (lambda (v)
>> ;; `(if ,p (setcdr ,p ,v)
>> ;; ,(funcall setter
>> ;; `(cons (cons ,k ,v) ,getter)))))))))))
>> ;; (cdr (assoc key alist)))
>>
> Good grief, does it have to be so complicated for such a simple need?
I think so, yes. CL's define-expander-macro would be a bit worse, but
otherwise comparable. The expander macro itself above is:
(macroexp-let2 macroexp-copyable-p k key
(gv-letplace (getter setter) alist
(macroexp-let2 nil p `(assoc ,k ,getter)
(funcall do `(cdr ,p)
(lambda (v)
`(if ,p (setcdr ,p ,v)
,(funcall setter
`(cons (cons ,k ,v) ,getter)))))))))))
and the code we want to generate for a `setf' is along the lines of:
(let* ((k KEY)
(a ALIST)
(p (assoc k (alist-getter a)))
(v VAL))
(if p (setcdr p v)
(alist-setter a (cons (cons k v) (alist-getter a)))))
so it's not too far from the optimum (all those let-binding of KEY to
`k' etc... are needed if KEY is an expression that could have
side-effects so we need to make sure it's evaluated exactly once).
Note that the above expander can be used for things like (push VAL
(alist-get KEY ALIST)) as well, where we want to generate code like
(let* ((k KEY)
(a ALIST)
(p (assoc k (alist-getter a)))
(v VAL))
(if p (setcdr p (cons v (cdr p)))
(alist-setter a (cons (cons k (cons v (cdr p)))
(alist-getter a)))))
Note that it doesn't work quite right for `letf', where
(letf (((alist-get KEY ALIST) VAL)) BODY)
gets expanded to:
ELISP> (macroexpand '(letf (((alist-get KEY ALIST) VAL)) BODY))
(let* ((p (assoc KEY ALIST))
(vnew VAL) (old (cdr p)))
(unwind-protect
(progn
(if p (setcdr p vnew) (setq ALIST (cons (cons KEY vnew) ALIST)))
BODY)
(if p (setcdr p old) (setq ALIST (cons (cons KEY old) ALIST)))))
So to be correct for `letf' we could use something like:
(defun alist-get (key alist)
"Get the value associated to KEY in ALIST."
(declare
(gv-expander
(lambda (do)
(macroexp-let2 macroexp-copyable-p k key
(gv-letplace (getter setter) alist
(macroexp-let2 nil p `(assoc ,k ,getter)
(funcall do `(cdr ,p)
(lambda (v)
`(if ,p (setcdr ,p ,v)
,(funcall setter
`(cons (setq ,p (cons ,k ,v))
,getter)))))))))))
(cdr (assoc key alist)))
But for most uses this extra `setq' would be at best useless and might
make the code less efficient (a var that's never `setq'd can be
captured into a closure much more cheaply because we can just keep
a copy of its value, rather than keeping a reference to the variable
itself and looking up its value everytime).
-- Stefan