summaryrefslogtreecommitdiff
path: root/devdocs/elisp/void-variables.html
blob: dfcd140de7bbd978abd4052b70c2a815a76cfe42 (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
43
44
45
46
47
48
49
50
51
 <h3 class="section">When a Variable is Void</h3>   <p>We say that a variable is void if its symbol has an unassigned value cell (see <a href="symbol-components">Symbol Components</a>). </p> <p>Under Emacs Lisp’s default dynamic scoping rule (see <a href="variable-scoping">Variable Scoping</a>), the value cell stores the variable’s current (local or global) value. Note that an unassigned value cell is <em>not</em> the same as having <code>nil</code> in the value cell. The symbol <code>nil</code> is a Lisp object and can be the value of a variable, just as any other object can be; but it is still a value. If a variable is void, trying to evaluate the variable signals a <code>void-variable</code> error, instead of returning a value. </p> <p>Under the optional lexical scoping rule, the value cell only holds the variable’s global value—the value outside of any lexical binding construct. When a variable is lexically bound, the local value is determined by the lexical environment; hence, variables can have local values even if their symbols’ value cells are unassigned. </p> <dl> <dt id="makunbound">Function: <strong>makunbound</strong> <em>symbol</em>
</dt> <dd>
<p>This function empties out the value cell of <var>symbol</var>, making the variable void. It returns <var>symbol</var>. </p> <p>If <var>symbol</var> has a dynamic local binding, <code>makunbound</code> voids the current binding, and this voidness lasts only as long as the local binding is in effect. Afterwards, the previously shadowed local or global binding is reexposed; then the variable will no longer be void, unless the reexposed binding is void too. </p> <p>Here are some examples (assuming dynamic binding is in effect): </p> <div class="example"> <pre class="example">(setq x 1)               ; <span class="roman">Put a value in the global binding.</span>
     ⇒ 1
(let ((x 2))             ; <span class="roman">Locally bind it.</span>
  (makunbound 'x)        ; <span class="roman">Void the local binding.</span>
  x)
error→ Symbol's value as variable is void: x
</pre>
<pre class="example">x                        ; <span class="roman">The global binding is unchanged.</span>
     ⇒ 1

(let ((x 2))             ; <span class="roman">Locally bind it.</span>
  (let ((x 3))           ; <span class="roman">And again.</span>
    (makunbound 'x)      ; <span class="roman">Void the innermost-local binding.</span>
    x))                  ; <span class="roman">And refer: it’s void.</span>
error→ Symbol's value as variable is void: x
</pre>

<pre class="example">(let ((x 2))
  (let ((x 3))
    (makunbound 'x))     ; <span class="roman">Void inner binding, then remove it.</span>
  x)                     ; <span class="roman">Now outer <code>let</code> binding is visible.</span>
     ⇒ 2
</pre>
</div> </dd>
</dl> <dl> <dt id="boundp">Function: <strong>boundp</strong> <em>variable</em>
</dt> <dd>
<p>This function returns <code>t</code> if <var>variable</var> (a symbol) is not void, and <code>nil</code> if it is void. </p> <p>Here are some examples (assuming dynamic binding is in effect): </p> <div class="example"> <pre class="example">(boundp 'abracadabra)          ; <span class="roman">Starts out void.</span>
     ⇒ nil
</pre>
<pre class="example">(let ((abracadabra 5))         ; <span class="roman">Locally bind it.</span>
  (boundp 'abracadabra))
     ⇒ t
</pre>
<pre class="example">(boundp 'abracadabra)          ; <span class="roman">Still globally void.</span>
     ⇒ nil
</pre>
<pre class="example">(setq abracadabra 5)           ; <span class="roman">Make it globally nonvoid.</span>
     ⇒ 5
</pre>
<pre class="example">(boundp 'abracadabra)
     ⇒ t
</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/Void-Variables.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Void-Variables.html</a>
  </p>
</div>