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

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

Re: DynamicBindingVsLexicalBinding


From: Barry Margolin
Subject: Re: DynamicBindingVsLexicalBinding
Date: Wed, 16 Oct 2013 10:26:54 -0400
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <mailman.4127.1381928277.10748.help-gnu-emacs@gnu.org>,
 phillip.lord@newcastle.ac.uk (Phillip Lord) wrote:

> Perhaps my understanding is not complete, but I always thought that
> dynamic and static binding was about variables that are unbound. If we
> have global variables with mutable state, then as far as I can see, you
> could do something equivalent with either.
> 
> So, this...
> 
> (defun perldoc ()
>   (interactive)
>   (require 'man)
>   (let ((manual-program "perldoc"))
>     (call-interactively 'man)))
> 
> is nearly equivalent to 
> 
> (defun perldoc()
>   (interactive)
>   (require 'man)
>   (let ((old manual-program))
>     (setq manual-program "perldoc")
>     (call-interactively 'man)
>     (setq manual-program old))
> 
> 
> If we were multi-threaded, then AFAICT, the let version would be
> cleaner -- as the altered manual-program is *only* in the dynamic
> environment of the call to the perldoc command. So another call to the
> man function would be unaffected. While the second changes global state,
> and then changes it back. But, we aren't.

You also need unwind-protect to ensure that the value is reverted if the 
code aborts.

With that fix, it's essentially how most implementations of dynamic 
binding worked in single-threaded Lisps. But multi-threaded Lisps need 
to add hooks into thread switching.

-- 
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]