Next: , Previous: , Up: Local Call   [Contents][Index]


5.6.3 Closures

Local call allows for much more efficient use of closures, since the closure environment doesn’t need to be allocated on the heap, or even stored in memory at all. In this example, there is no penalty for localfun referencing a and b:

(defun foo (a b)
  (flet ((localfun (x)
           (1+ (* a b x))))
    (if (= a b)
        (localfun (- x))
        (localfun x))))

In local call, the compiler effectively passes closed-over values as extra arguments, so there is no need for you to “optimize” local function use by explicitly passing in lexically visible values. Closures may also be subject to let optimization (see let-optimization.)

Note: indirect value cells are currently always allocated on the heap when a variable is both assigned to (with setq or setf) and closed over, regardless of whether the closure is a local function or not. This is another reason to avoid setting variables when you don’t have to.