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: Joost Kremers
Subject: Re: Question about let binding behavior
Date: Tue, 08 Oct 2024 10:15:11 +0200

On Tue, Oct 08 2024, Louis-Guillaume Gagnon wrote:
> 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.

The reason for that is that the list is a constant: the literal list is
part of the code, and the object is created when the defun is evaluated.
The object will stick around, even if the binding to it (baz) is local to
the function.

Now, what I honestly don't know is if the object can at some point be
garbage-collected, but I suspect it can. I wouldn't bet on it staying
around, anyway.

If you don't want the object to stick around, make sure you create it in
the function:

```
(defun foo (bar)
  (let ((baz (list (cons 'quux 0) (cons 'quuz 0))))
    (if (> 10 bar)
        (setcdr (assoc 'quux baz) bar)
      (setcdr (assoc 'quuz baz) bar))
    baz))
```



-- 
Joost Kremers
Life has its moments



reply via email to

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