[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: sending function arguments to recursive function calls
From: |
Dmitry Gutov |
Subject: |
Re: sending function arguments to recursive function calls |
Date: |
Sun, 19 May 2013 20:57:44 +0400 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 |
On 17.05.2013 18:31, Drew Adams wrote:
This is the ugly side of dynamic scoping.
(defun foo () (let ((bar 42)) (baz)))
(defun baz () bar)
(foo) ; => 42
baz ; => void-variable error
Huh? I guess you meant to write
(baz) ; => "void-variable bar" error
No, I meant exactly to write variable reference, not function call, to
illustrate that it's not defined globally.
There is nothing ugly about that behavior.
It's ugly because this kind of code is hard to reason about and,
consequently, hard to modify. Suppose I want to rewrite `foo' (and
suppose it's longer than this one line).
Can I rename `bar' to something else? No idea: to be absolutely sure, I
have to search the definitions of all functions that `foo' calls, and if
I find a `bar' reference in any of them, I'll now have to search for any
other functions that call them, etc. IOW, this makes for terrible
composability.
The behavior is ugly because it allows the code to be written this way.
A worse example is when `bar' is one of the arguments to `foo' (ugh).
The `let' binds variable `bar' for the dynamic extent of the call to `foo'.
There is no other binding of `bar' or assignment to it here, so `(baz)' refers
to an unbound variable `bar'.
What happens with lexical scoping?
(foo) ; => "void-variable bar" error
(baz) ; => "void-variable bar" error
As it should. Contrast this with the situation when `bar' has been
`defvar'ed in advance. Both functions would know that this var is
global, so if it's renamed in some place, it definitely should be
renamed in all functions that reference it.
This is what I can call the light side of dynamic scoping, and it's how
the term "dynamic binding" is often defined.
Dynamic binding facilitates user extension ("monkey patching"). And yes, this
is particularly important for a dynamic user environment like Emacs.
It is easy to find references lauding the benefits of lexical binding (most
languages use only lexical binding). Stallman explains well why dynamic binding
is important for Emacs:
http://www.gnu.org/software/emacs/emacs-paper.html#SEC17.
I could offer some criticism for the paper, but there's really no need.
Just recall that Emacs is on track to eventually replace dynamic scoping
with lexical scoping everywhere, with exceptions for defvar'ed vars
(controlled dynamic binding), and nobody is really arguing that Emacs
will become too hard to customize as a result. Nobody reasonably
well-informed, anyway.
- Re: sending function arguments to recursive function calls, (continued)
- Re: sending function arguments to recursive function calls, Gauthier Östervall, 2013/05/07
- RE: sending function arguments to recursive function calls, Drew Adams, 2013/05/07
- Re: sending function arguments to recursive function calls, Stefan Monnier, 2013/05/08
- Re: sending function arguments to recursive function calls, Gauthier Östervall, 2013/05/09
- Re: sending function arguments to recursive function calls, Stefan Monnier, 2013/05/09
- Re: sending function arguments to recursive function calls, Gauthier Östervall, 2013/05/12
- Re: sending function arguments to recursive function calls, Stefan Monnier, 2013/05/13
- Re: sending function arguments to recursive function calls, Gauthier Östervall, 2013/05/17
- Re: sending function arguments to recursive function calls, Dmitry Gutov, 2013/05/17
- RE: sending function arguments to recursive function calls, Drew Adams, 2013/05/17
- Re: sending function arguments to recursive function calls,
Dmitry Gutov <=
- RE: sending function arguments to recursive function calls, Drew Adams, 2013/05/21
- Message not available
- Re: sending function arguments to recursive function calls, Pascal J. Bourguignon, 2013/05/19
- Re: sending function arguments to recursive function calls, Dmitry Gutov, 2013/05/20
- Message not available
- Re: sending function arguments to recursive function calls, Pascal J. Bourguignon, 2013/05/20
- Re: sending function arguments to recursive function calls, Pascal J. Bourguignon, 2013/05/07
- Message not available
- Re: sending function arguments to recursive function calls, Pascal J. Bourguignon, 2013/05/07
- Re: sending function arguments to recursive function calls, Stefan Monnier, 2013/05/08
Re: sending function arguments to recursive function calls, Stefan Monnier, 2013/05/04