Subject: Re: Determining lexically bound variables inside a macro
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 04 Aug 2004 00:07:34 -0500
Newsgroups: comp.lang.lisp
Message-ID: <X9qdnQyd2LGL8o3cRVn-jw@speakeasy.net>
Aneil Mallavarapu <aneil.mallavarapu@gmail.com> wrote:
+---------------
| Of course, one could stick to the *VAR* naming convention, but I'd
| like to avoid this for aesthetic reasons.
+---------------

Please don't avoid it, at least, not for any variables that are truly
dynamically bound *anywhere*. It will only make your code hard to maintain
by others. The *VAR* convention is, at this point, so pervasive in the
community that it's almost mandatory (as is +NAME+ for global constants).

As Kent Pitman is well-known for saying[1], a language is really defined
as much by its community as by the standard (if any), a point that should
not be ignored when extending said language. The jury is still out on
whether "global lexicals" really have a proper place in Common Lisp,
and if so, what the best way to implement them is.[2] I say this as
one who has more than once tried to find "the right way" to fit
"global lexicals" into Common Lisp (since, coming from Scheme, it
initially seemed like a good idea). But I'm still not sure there is
one that doesn't violate the community expectations of what is implied
by the normal variable naming conventions.

Yes, I have my own personal DEFLEX macro, and yes, I use it a lot
when doing exploratory programming interactively at the REPL, but I
don't use it at *all* in actual source code I write. Besides a few
subtle bugs[3], it just doesn't seem to feel stylistically correct
for wide use in Common Lisp (at least not yet).

In summary, I would suggest *not* changing all your globals to
"lexical globals" just because you find *VAR* "unaesthetic".
Try it out for a while in some corner of the code (which nevertheless
gets edited by more than one person) to see whether it passes the
dual tests of robustness and the principle of least astonishment
to your fellow coders.


-Rob

[1] <http://www.nhplace.com/kent/PS/Lambda.html>

[2] Pascal's simple suggestion, while fine for discussion, doesn't
    actually suffice in practice. E.g., what happens if you have
    said (DEFINE-SYMBOL-MACRO MY-VAR *MY-VAR*) and somebody elsewhere
    in the code also does a (DEFVAR *MY-VAR* xxx) and then SETQs it?
    Oops. You need to do this with some sort of DEFLEX macro that
    automatically munges the underlying variable name so that such
    collisions don't happen [or are at least *very* unlikely].

[3] It didn't always do quite the right thing under CMUCL when used
    in some "place expressions" (SETF, INCF, etc.). I haven't yet
    figured out whether the problem is my macro or in CMUCL's handling
    of symbol macros in place expressions. ("Film at 11...")

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607