diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/function-indirection.html | |
new repository
Diffstat (limited to 'devdocs/elisp/function-indirection.html')
| -rw-r--r-- | devdocs/elisp/function-indirection.html | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/devdocs/elisp/function-indirection.html b/devdocs/elisp/function-indirection.html new file mode 100644 index 00000000..132a0f50 --- /dev/null +++ b/devdocs/elisp/function-indirection.html @@ -0,0 +1,42 @@ + <h4 class="subsection">Symbol Function Indirection</h4> <p>If the first element of the list is a symbol then evaluation examines the symbol’s function cell, and uses its contents instead of the original symbol. If the contents are another symbol, this process, called <em>symbol function indirection</em>, is repeated until it obtains a non-symbol. See <a href="function-names">Function Names</a>, for more information about symbol function indirection. </p> <p>One possible consequence of this process is an infinite loop, in the event that a symbol’s function cell refers to the same symbol. Otherwise, we eventually obtain a non-symbol, which ought to be a function or other suitable object. </p> <p>More precisely, we should now have a Lisp function (a lambda expression), a byte-code function, a primitive function, a Lisp macro, a special form, or an autoload object. Each of these types is a case described in one of the following sections. If the object is not one of these types, Emacs signals an <code>invalid-function</code> error. </p> <p>The following example illustrates the symbol indirection process. We use <code>fset</code> to set the function cell of a symbol and <code>symbol-function</code> to get the function cell contents (see <a href="function-cells">Function Cells</a>). Specifically, we store the symbol <code>car</code> into the function cell of <code>first</code>, and the symbol <code>first</code> into the function cell of <code>erste</code>. </p> <div class="example"> <pre class="example">;; <span class="roman">Build this function cell linkage:</span> +;; ------------- ----- ------- ------- +;; | #<subr car> | <-- | car | <-- | first | <-- | erste | +;; ------------- ----- ------- ------- +</pre> +<pre class="example">(symbol-function 'car) + ⇒ #<subr car> +</pre> +<pre class="example">(fset 'first 'car) + ⇒ car +</pre> +<pre class="example">(fset 'erste 'first) + ⇒ first +</pre> +<pre class="example">(erste '(1 2 3)) ; <span class="roman">Call the function referenced by <code>erste</code>.</span> + ⇒ 1 +</pre> +</div> <p>By contrast, the following example calls a function without any symbol function indirection, because the first element is an anonymous Lisp function, not a symbol. </p> <div class="example"> <pre class="example">((lambda (arg) (erste arg)) + '(1 2 3)) + ⇒ 1 +</pre> +</div> <p>Executing the function itself evaluates its body; this does involve symbol function indirection when calling <code>erste</code>. </p> <p>This form is rarely used and is now deprecated. Instead, you should write it as: </p> <div class="example"> <pre class="example">(funcall (lambda (arg) (erste arg)) + '(1 2 3)) +</pre> +</div> <p>or just </p> +<div class="example"> <pre class="example">(let ((arg '(1 2 3))) (erste arg)) +</pre> +</div> <p>The built-in function <code>indirect-function</code> provides an easy way to perform symbol function indirection explicitly. </p> <dl> <dt id="indirect-function">Function: <strong>indirect-function</strong> <em>function &optional noerror</em> +</dt> <dd> +<p>This function returns the meaning of <var>function</var> as a function. If <var>function</var> is a symbol, then it finds <var>function</var>’s function definition and starts over with that value. If <var>function</var> is not a symbol, then it returns <var>function</var> itself. </p> <p>This function returns <code>nil</code> if the final symbol is unbound. It signals a <code>cyclic-function-indirection</code> error if there is a loop in the chain of symbols. </p> <p>The optional argument <var>noerror</var> is obsolete, kept for backward compatibility, and has no effect. </p> <p>Here is how you could define <code>indirect-function</code> in Lisp: </p> <div class="example"> <pre class="example">(defun indirect-function (function) + (if (and function + (symbolp function)) + (indirect-function (symbol-function function)) + function)) +</pre> +</div> </dd> +</dl><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 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/Function-Indirection.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Indirection.html</a> + </p> +</div> |
