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

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

Re: Question about let binding behavior


From: Michael Heerdegen
Subject: Re: Question about let binding behavior
Date: Wed, 09 Oct 2024 00:21:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Louis-Guillaume Gagnon <gagnonlg@protonmail.com> writes:

> That makes sense, but I guess I'm more surprised that the list itself
> is only evaluated a single time -- I would naively have expected for
> the list to be created anew every time the function is called but
> that's evidently not what's happening.

Some background: This is one of the most surprising things in Elisp.
Using literal lists in code can produce hard to detect bugs.  The
phenomenon has the name "self modifying code".

BTW, considering evaluation alone doesn't suffice to understand what is
going on here - we have to step back and start with the Lisp reader.
Reading the expression

  '((quux . 0) (quuz . 0))

will create a quoted list literal: it is the reader that creates the
list ((quux . 0) (quuz . 0)) _once_.  This list literal gets part of
your code, with a quote in front of it.  That quote prevents evaluation.

The weird thing here is that when the code is running it has access to
that list that is part of the code at the same time.  The object is part
of two different levels.

Please think about it and try hard not to forget: (list ELEMENT ...) and
'(ELEMENT) are _not_ equivalent, the latter is not simply an
abbreviation of the former.  Whenever you use a quoted list in your
code, be sure to avoid this pitfall.  If your list literal might be
modified destructively when running the code, maybe even as part of a
larger structure (as a sublist maybe), better let your code construct a
fresh list.

Also keep in mind that `backquote'd expressions also create list
literals.  There is an analogue pitfall for backquote.


Michael.




reply via email to

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