help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: alist-get in Emacs 24?


From: Drew Adams
Subject: RE: alist-get in Emacs 24?
Date: Wed, 7 Oct 2015 13:45:11 -0700 (PDT)

> I have to pass the alist variable name as a symbol.  (This is needed
> because I might want to use it to introduce a new key-value pair
> into the list.)  Does anyone have any suggestion for enhancing my
> solution?
> 
> (defun my-set-alist-value (key alist value)
>   "Set the value corresponding to KEY in ALIST to VALUE.
> Note: ALIST should be a symbol.  This is morally equivalent to
> `(setf (alist-get key (symbol-value alist)) value)',
> but works in older Emacsen."
>   (let ((pair (assoc key (symbol-value alist))))
>     (if pair
>       (setcdr pair value)
>       (set alist (acons key value (symbol-value alist))))))

You say that ALIST must be a symbol here, but I don't see why.
Why not just modify the alist structure (i.e., the alist) itself?
Why repoint the variable to the result inside the set function?

If you modify the structure in place, and you return the new alist,
then you can always do this to ensure that the variable is up-to-date:
(setq THE-VAR (my-set-alist-value key alist value))

That's typically the way destructive operations are done.  It is
why you need to do (setq foo (delete 42 foo)).



reply via email to

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