Next: , Previous: , Up: Variable Scoping   [Contents][Index]

16.8.2 Extent

Extent refers to the time during program execution that a variable name is valid. In SXEmacs Lisp, a variable is valid only while the form that bound it is executing. This is called dynamic extent. “Local” or “automatic” variables in most languages, including C and Pascal, have dynamic extent.

One alternative to dynamic extent is indefinite extent. This means that a variable binding can live on past the exit from the form that made the binding. Common Lisp and Scheme, for example, support this, but SXEmacs Lisp does not.

To illustrate this, the function below, make-add, returns a function that purports to add n to its own argument m. This would work in Common Lisp, but it does not work as intended in SXEmacs Lisp, because after the call to make-add exits, the variable n is no longer bound to the actual argument 2.

(defun make-add (n)
    (function (lambda (m) (+ n m))))  ; Return a function.
     ⇒ make-add
(fset 'add2 (make-add 2))  ; Define function add2
                           ;   with (make-add 2).
     ⇒ (lambda (m) (+ n m))
(add2 4)                   ; Try to add 2 to 4.
error→ Symbol's value as variable is void: n

Some Lisp dialects have “closures”, objects that are like functions but record additional variable bindings. SXEmacs Lisp does not have closures.