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

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

Re: Declaring a local dynamic variable?


From: Barry Margolin
Subject: Re: Declaring a local dynamic variable?
Date: Fri, 27 Sep 2013 10:31:19 -0400
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <mailman.2982.1380275511.10748.help-gnu-emacs@gnu.org>,
 Andreas Röhler <andreas.roehler@easy-emacs.de> wrote:

> Am 25.09.2013 20:53, schrieb Stefan Monnier:
> >>>> So in essence Emacs doesn't really have local dynamic variables?
> >>> Dynamic scoping is inherently global, whether in Elisp or in any other
> >>> language.
> >
> >> Not in Common Lisp.
> >
> >> (defun d1 ()
> >>    (let ((var 1))
> >>      (declare (special var))
> >>      (l1)))
> >
> >> (defun l1 ()
> >>    (let ((var 2))
> >>      (print var)
> >>      (d2)))
> >
> >> (defun d2 ()
> >>    (declare (special var))
> >>    (print var))
> >
> >> D1 and D2 use the dynamic variable VAR, L1 uses the lexical variable VAR.
> >
> > Same in Elisp.  But if d1 and d2 are in two separate packages that know
> > nothing about each other but who happen to call each other through some
> > potentially twisted sequence of calls, they will still interfere,
> > because there's only (globally) one dynamic variable by that name.
> >
> 
> Now if two separate packages define a lexical var with the very same, is such 
> a confusion excluded?

Yes. By definition, lexical variables are only visible in the lexical 
contour, not in other functions.

The exception is if a macro expands into code that binds the variable:

(defmacro pkg1-m (&body b)
  `(let ((var 'm))
     ,@b))

(defun pkg2-f ()
  (let ((var 'f))
    (pkg1-m
      (print var))))

(pkg2-f) will print "m" rather than "f".

This is generally referred to as the "hygiene" problem in macros. It's 
why macros should use gensym to create the local variables used in the 
expansion, or take the variable as a parameter.  Dialects like Scheme 
have built-in mechanisms to create hygienic macros.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


reply via email to

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