summaryrefslogtreecommitdiff
path: root/devdocs/elisp/accessing-variables.html
blob: abbe980313c99d56a1b2a3cda3e1de099c16d363 (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
 <h3 class="section">Accessing Variable Values</h3> <p>The usual way to reference a variable is to write the symbol which names it. See <a href="symbol-forms">Symbol Forms</a>. </p> <p>Occasionally, you may want to reference a variable which is only determined at run time. In that case, you cannot specify the variable name in the text of the program. You can use the <code>symbol-value</code> function to extract the value. </p> <dl> <dt id="symbol-value">Function: <strong>symbol-value</strong> <em>symbol</em>
</dt> <dd>
<p>This function returns the value stored in <var>symbol</var>’s value cell. This is where the variable’s current (dynamic) value is stored. If the variable has no local binding, this is simply its global value. If the variable is void, a <code>void-variable</code> error is signaled. </p> <p>If the variable is lexically bound, the value reported by <code>symbol-value</code> is not necessarily the same as the variable’s lexical value, which is determined by the lexical environment rather than the symbol’s value cell. See <a href="variable-scoping">Variable Scoping</a>. </p> <div class="example"> <pre class="example">(setq abracadabra 5)
     ⇒ 5
</pre>
<pre class="example">(setq foo 9)
     ⇒ 9
</pre>

<pre class="example">;; <span class="roman">Here the symbol <code>abracadabra</code></span>
;;   <span class="roman">is the symbol whose value is examined.</span>
(let ((abracadabra 'foo))
  (symbol-value 'abracadabra))
     ⇒ foo
</pre>

<pre class="example">;; <span class="roman">Here, the value of <code>abracadabra</code>,</span>
;;   <span class="roman">which is <code>foo</code>,</span>
;;   <span class="roman">is the symbol whose value is examined.</span>
(let ((abracadabra 'foo))
  (symbol-value abracadabra))
     ⇒ 9
</pre>

<pre class="example">(symbol-value 'abracadabra)
     ⇒ 5
</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/Accessing-Variables.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Accessing-Variables.html</a>
  </p>
</div>