emacs-devel
[Top][All Lists]
Advanced

[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: Thu, 30 Aug 2012 20:59:33 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2.50 (gnu/linux)

> (setq lexical-binding t)

BTW, don't do that: instead, set it as a file-local variable with
"-*- lexical-binding: t -*-".

> (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))))))))))))

The (declare ...) form is used to give auxiliary info about the function
you're defining (such as info about how to indent it, how Edebug needs
to instrument it, or in our case how `setf' or `push' can "set" it).
The function itself ignores it.  So the definition above is like

   (defun alist-get (key alist)
     "Get the value associated to KEY in ALIST.")

I.e. you removed the body of the function (which was the last line:

   (cdr (assoc key alist))

).
     
> Yes this is what needs to be generated.  I don't understand why there
> are two lambdas in alist-get.

See C-h f gv-define-expander RET and `gv-get'.

> Is there any chance you would uncomment this function so that I can use
> it in my code?

There's a chance, but if you could explain why (push (cons KEY VAL) ALIST)
doesn't work for you, that would help,


        Stefan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]