Undefined values: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: Discuss local rebinding.)
Line 157: Line 157:
(setf *y* 43) ;; *y* is bound again.</lang>
(setf *y* 43) ;; *y* is bound again.</lang>


By contrast, lexical variables never lack a binding. Without an initializer, they are initialized to nil.
The same goes for local re-binding of special variables:

<lang lisp>
(defvar *dyn*) ;; special, no binding

(let (*dyn* ;; locally scoped override, value is nil
lex) ;; lexical, value is nil
(list (boundp '*dyn*) *dyn* (boundp 'lex) lex)) -> (T NIL NIL NIL)

(boundp '*global*) -> NIL</lang>

Here we can see that inside the scope of the let, the special variable has a binding (to the value <code>NIL</code>) and so <code>(boundp '*dyn*)</code> yields <code>T</code>. But <code>boundp</code> does not "see" the lexical variable <code>lex</code>; it reports that <code>lex</code> is unbound. Local binding constructs never leave a variable without a value, be it dynamic or lexical: both <code>*dyn*</code> and <code>lex</code> evaluate to NIL, but using very different mechanisms.


=={{header|D}}==
=={{header|D}}==