summaryrefslogtreecommitdiff
path: root/devdocs/elisp/surprising-local-vars.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2025-08-14 22:58:58 -0500
committerCraig Jennings <c@cjennings.net>2025-08-14 22:58:58 -0500
commit82ba818ff456bcd6d56a06226e3f27e98fbb55c3 (patch)
tree158cfc17b2f644a10f063cb546752cfaae12c97f /devdocs/elisp/surprising-local-vars.html
parent9278ddd4ea1a8b1a4c1edaa8894516e3f48d245b (diff)
downloaddotemacs-82ba818ff456bcd6d56a06226e3f27e98fbb55c3.tar.gz
dotemacs-82ba818ff456bcd6d56a06226e3f27e98fbb55c3.zip
removing all downloaded devdocs files
Diffstat (limited to 'devdocs/elisp/surprising-local-vars.html')
-rw-r--r--devdocs/elisp/surprising-local-vars.html30
1 files changed, 0 insertions, 30 deletions
diff --git a/devdocs/elisp/surprising-local-vars.html b/devdocs/elisp/surprising-local-vars.html
deleted file mode 100644
index 37947ea1b..000000000
--- a/devdocs/elisp/surprising-local-vars.html
+++ /dev/null
@@ -1,30 +0,0 @@
- <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>