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

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

bug#9469: buffer-local variables seem to remember previous values


From: Drew Adams
Subject: bug#9469: buffer-local variables seem to remember previous values
Date: Sat, 10 Sep 2011 10:44:19 -0700

> 1. emacs -Q
> 2. eval this region:
> (setq buf-a (create-file-buffer "a"))
> (setq foo nil)
> (make-variable-buffer-local 'foo)
> (defun test1 ()
>   (interactive)
>   (let (alist)
>     (push '(:var . 0) alist)
>     (with-current-buffer buf-a
>       (setq foo alist))))
> (defun test2 ()
>   (interactive)
>   (with-current-buffer buf-a
>     (setcdr (assq :var foo) 20)))
> (defun show ()
>   (interactive)
>   (with-current-buffer buf-a
>     (format "    ; foo in 'a' is %s" foo)))
> (defun test3 ()
>   (interactive)
>   (let (alist)
>     (push `(:var . ,(+ 0)) alist)
>     (with-current-buffer buf-a
>       (setq foo alist))))
> 
> (test1)
> (test2)
> (test1)
> (insert (show))
> (test3)
> (insert (show))
> 
> Note results on both `insert' lines should be identical but the first
> insert some how remembers a previous value.  I find it surprising that
> no one has ever come across this before.

No, they should not be identical.  This is a classic Lisp gotcha.

(test1) sets buffer-local var `foo' to a new alist ((:var . 0)).
(test2) sets the cdr of the single element of that alist to 20.
That means that `foo' in buf-a is now ((:var . 20)).
(test1) then creates a new alist and pushes the _same_ cons, (:var . 20) onto
it.
And it sets `foo' to this new alist.

If you were to use (cons :var 0) instead of '(:var . 0) then you would not be
reusing the same cons cell.

Note that different Lisps (and different implementations of the same Lisp) can
treat a sexp such as '(a b) differently - they might or might not create a new
list each time it is read or eval'd.

To be sure to get what you expect in situations like this, do not use '(...).
Use `cons' or `list' or equivalent backquote syntax.  Do not expect '(...) to
create new list structure each time it is read/eval'd.






reply via email to

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