summaryrefslogtreecommitdiff
path: root/devdocs/elisp/closures.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/elisp/closures.html')
-rw-r--r--devdocs/elisp/closures.html10
1 files changed, 10 insertions, 0 deletions
diff --git a/devdocs/elisp/closures.html b/devdocs/elisp/closures.html
new file mode 100644
index 00000000..3e8a42b3
--- /dev/null
+++ b/devdocs/elisp/closures.html
@@ -0,0 +1,10 @@
+ <h3 class="section">Closures</h3> <p>As explained in <a href="variable-scoping">Variable Scoping</a>, Emacs can optionally enable lexical binding of variables. When lexical binding is enabled, any named function that you create (e.g., with <code>defun</code>), as well as any anonymous function that you create using the <code>lambda</code> macro or the <code>function</code> special form or the <code>#'</code> syntax (see <a href="anonymous-functions">Anonymous Functions</a>), is automatically converted into a <em>closure</em>. </p> <p>A closure is a function that also carries a record of the lexical environment that existed when the function was defined. When it is invoked, any lexical variable references within its definition use the retained lexical environment. In all other respects, closures behave much like ordinary functions; in particular, they can be called in the same way as ordinary functions. </p> <p>See <a href="lexical-binding">Lexical Binding</a>, for an example of using a closure. </p> <p>Currently, an Emacs Lisp closure object is represented by a list with the symbol <code>closure</code> as the first element, a list representing the lexical environment as the second element, and the argument list and body forms as the remaining elements: </p> <div class="example"> <pre class="example">;; <span class="roman">lexical binding is enabled.</span>
+(lambda (x) (* x x))
+ ⇒ (closure (t) (x) (* x x))
+</pre>
+</div> <p>However, the fact that the internal structure of a closure is exposed to the rest of the Lisp world is considered an internal implementation detail. For this reason, we recommend against directly examining or altering the structure of closure objects. </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/Closures.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html</a>
+ </p>
+</div>