summaryrefslogtreecommitdiff
path: root/devdocs/elisp/surprising-local-vars.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/surprising-local-vars.html
new repository
Diffstat (limited to 'devdocs/elisp/surprising-local-vars.html')
-rw-r--r--devdocs/elisp/surprising-local-vars.html30
1 files changed, 30 insertions, 0 deletions
diff --git a/devdocs/elisp/surprising-local-vars.html b/devdocs/elisp/surprising-local-vars.html
new file mode 100644
index 00000000..37947ea1
--- /dev/null
+++ b/devdocs/elisp/surprising-local-vars.html
@@ -0,0 +1,30 @@
+ <h4 class="subsection">Local Variables in Macro Expansions</h4> <p>In the previous section, the definition of <code>for</code> was fixed as follows to make the expansion evaluate the macro arguments the proper number of times: </p> <div class="example"> <pre class="example">(defmacro for (var from init to final do &amp;rest body)
+ "Execute a simple for loop: (for i from 1 to 10 do (print i))."
+</pre>
+<pre class="example"> `(let ((,var ,init)
+ (max ,final))
+ (while (&lt;= ,var max)
+ ,@body
+ (inc ,var))))
+</pre>
+</div> <p>The new definition of <code>for</code> has a new problem: it introduces a local variable named <code>max</code> which the user does not expect. This causes trouble in examples such as the following: </p> <div class="example"> <pre class="example">(let ((max 0))
+ (for x from 0 to 10 do
+ (let ((this (frob x)))
+ (if (&lt; max this)
+ (setq max this)))))
+</pre>
+</div> <p>The references to <code>max</code> inside the body of the <code>for</code>, which are supposed to refer to the user’s binding of <code>max</code>, really access the binding made by <code>for</code>. </p> <p>The way to correct this is to use an uninterned symbol instead of <code>max</code> (see <a href="creating-symbols">Creating Symbols</a>). The uninterned symbol can be bound and referred to just like any other symbol, but since it is created by <code>for</code>, we know that it cannot already appear in the user’s program. Since it is not interned, there is no way the user can put it into the program later. It will never appear anywhere except where put by <code>for</code>. Here is a definition of <code>for</code> that works this way: </p> <div class="example"> <pre class="example">(defmacro for (var from init to final do &amp;rest body)
+ "Execute a simple for loop: (for i from 1 to 10 do (print i))."
+ (let ((tempvar (make-symbol "max")))
+ `(let ((,var ,init)
+ (,tempvar ,final))
+ (while (&lt;= ,var ,tempvar)
+ ,@body
+ (inc ,var)))))
+</pre>
+</div> <p>This creates an uninterned symbol named <code>max</code> and puts it in the expansion instead of the usual interned symbol <code>max</code> that appears in expressions ordinarily. </p><div class="_attribution">
+ <p class="_attribution-p">
+ Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
+ <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Surprising-Local-Vars.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Surprising-Local-Vars.html</a>
+ </p>
+</div>