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: W. Greenhouse
Subject: Re: Declaring a local dynamic variable?
Date: Sat, 28 Sep 2013 07:21:01 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Andreas Röhler <address@hidden> writes:

[...]

> BTW not sure if we are speaking at the same thing, resp. if this might
> be expressed by "lexical" or "dynamic" already.
>
> IMO it's convenient to use and change a let-bound var in downward
> functions, whithout the need to hand it over via arguments etc.  Just
> a convenience, it saves keystrokes, a major reason for me to use
> Emacs.
>
> Maybe "let" should already provide what "declare" does?

You're right, maybe we don't quite understand each other.

To reiterate:

As it stands now, the byte compiler throws a warning when it sees a
function refer to a variable which is neither bound inside the function
(e.g. by `let', by the function's arglist, etc.) nor defined globally.
Variables bound by `let' do not throw the warning.  And this is not a
"dynamic" vs. "lexical" problem--the variables in question are already
dynamic for sure, because, not being bound inside the text of their
function, they can't possibly be lexically bound!  This situation is
analogous to the failed call to `getx' in the example under the second
paragraph of (info "(elisp) Lexical Binding").

So the warning is there for good reason.  When you're using dynamic
binding in your library, the warning tells you that you possibly made a
typo and should check to make sure you aren't setting the wrong thing.
Where you're using lexical binding, it tells you that your function
which refers to variable `foo' will certainly fail to pick up the
lexically bound `foo' in the calling function.  Either way, it is
something you probably want to know about.

You are of course able to use dynamic variables to pass state around
between two functions, even in a library that uses lexical-binding.  The
compiler's just going to complain about you doing so with, e.g., `setq'
without declaring the variable first.  The point is also made in (info
"(elisp) Dynamic Binding Tips") that, since such "utility" dynamic
variables are accessible to all of Emacs, they should be usefully named
to avoid collisions with similar functions, and maybe even have a
docstring.  Nudging the programmer to use `defvar' or `declare' for
these dynamic "utility" variables therefore seems appropriate.

-- 
Regards,
WGG




reply via email to

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