I encountered an issue with the following code, provided by NickD as an answer to a previous question (link to the question):
(defun my-add-string-to-list (LIST-VAR STRING)
(add-to-list LIST-VAR STRING t)
(buffer-string)) ;; just to show that the function does not return a list
(defun my-failing-test ()
(interactive)
(let ((MYLIST '()))
(my-add-string-to-list 'MYLIST "foo")
(my-add-string-to-list 'MYLIST "bar")
(message (format "%S" MYLIST))))
When I evaluate these functions in Emacs using eval-defun (C-M-x) in the *scratch* buffer and then run M-x my-failing-test, I encounter the following error (this happens also if I start Emacs with emacs -Q):
my-add-string-to-list: Symbol’s value as variable is void: MYLIST
Observations:
If I evaluate the
*scratch*buffer witheval-buffer, themy-failing-testfunction works as expected.If I save the same code (as suggested by NickD) in a file, for example,
/tmp/fool.el, and then evaluate the functions witheval-defun, themy-failing-testfunction works as expected.
Additional information:
- The modeline in the
*scratch*buffer reads:(Lisp Interaction ElDoc). - The modeline in the
fool.elbuffer reads:(Elisp/d ElDoc).
Why is this happening? Until now, I've always used the scratch buffer to test function prototypes, and I believed that was its purpose. Am I mistaken, or is this some sort of bug?