all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Le Wang'" <l26wang@gmail.com>, <9469@debbugs.gnu.org>
Subject: bug#9469: buffer-local variables seem to remember previous values
Date: Sat, 10 Sep 2011 10:44:19 -0700	[thread overview]
Message-ID: <70AD30BE45C847BE828A3E8A71280A8E@us.oracle.com> (raw)
In-Reply-To: <CAM=K+ipbSAqy1PLo=qHRb_SqtKU+M6B1RRDv_LPym3P1ir2W4Q@mail.gmail.com>

> 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	other threads:[~2011-09-10 17:44 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-10 17:07 bug#9469: buffer-local variables seem to remember previous values Le Wang
2011-09-10 17:44 ` Drew Adams [this message]
2011-09-11 16:57   ` Le Wang
2011-09-11 17:56     ` Drew Adams
2011-09-11 18:08       ` Andreas Schwab
2011-09-11 18:49         ` Le Wang
2011-09-11 18:54           ` Lars Magne Ingebrigtsen
2011-09-11 19:18             ` Le Wang
2011-09-11 19:54               ` Lars Magne Ingebrigtsen
2011-09-12  4:39                 ` Le Wang
2011-09-12  7:57                   ` Andreas Schwab
2011-09-12  8:23                     ` Le Wang
2011-09-12  8:35                       ` Andreas Schwab
2011-09-12 14:30                         ` Drew Adams
2011-09-12 15:06                         ` Le Wang
2011-09-13 12:54                   ` Stefan Monnier
2011-09-13 13:12                     ` Le Wang
2011-09-13 15:00                     ` Helmut Eller
2011-09-13 18:02                     ` Johan Bockgård
2011-09-13 18:52                       ` Drew Adams
2011-09-13 20:42                         ` Helmut Eller
2011-09-15 18:14                       ` Le Wang
2011-09-11 19:37             ` Helmut Eller
2011-09-11 19:46           ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=70AD30BE45C847BE828A3E8A71280A8E@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=9469@debbugs.gnu.org \
    --cc=l26wang@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.