help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to distinguish gobal and local variables in elisp?


From: Kevin Rodgers
Subject: Re: How to distinguish gobal and local variables in elisp?
Date: Thu, 21 Sep 2006 13:51:13 -0600
User-agent: Thunderbird 1.5.0.7 (Windows/20060909)

jronald wrote:
The question comes from setq.
Usually, setq appears in a file without in any parentheses.
Does it mean that it awlays set the global varaible then? Or what's a local variable in lisp?

Emacs Lisp is dynamically scoped (like most older Lisp dialects, and unlike most newer dialects). That means that a variable may be
referenced outside the lexical scope that declares it.  For example:

(setq foo 0)

;; At this point, foo has only a global binding, to 0.

(defun bar ()
  (setq foo 2))

;; When bar is called, it will update foo's global binding, unless it is
;; shadowed by a local binding.

(let ((foo 1))
  ;; For the duration of the let form, foo also has a local binding.
  ;; At this point, its local value is 1.
  (bar)
  ;; Now foo's local value is 2, but its global value is still 0.
  )

;; And now only the global binding exists, so foo's value is 0.

--
Kevin





reply via email to

[Prev in Thread] Current Thread [Next in Thread]