summaryrefslogtreecommitdiff
path: root/devdocs/elisp/function-indirection.html
blob: 132a0f50dca9d31573f012b511790a3e7a4b97ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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>
;;   -------------       -----        -------        -------
;;  | #&lt;subr car&gt; | &lt;-- | car |  &lt;-- | first |  &lt;-- | erste |
;;   -------------       -----        -------        -------
</pre>
<pre class="example">(symbol-function 'car)
     ⇒ #&lt;subr car&gt;
</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 &amp;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 &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/Function-Indirection.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Indirection.html</a>
  </p>
</div>