Subject: Re: lambda-returning defmacro, capture
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 13 Dec 2006 06:47:00 -0600
Newsgroups: comp.lang.lisp
Message-ID: <V8-dne98COvZa-LYnZ2dnUVZ_v2nnZ2d@speakeasy.net>
<to9sn2r02@sneakemail.com> wrote:
+---------------
| What's the correct way to define a global without getting
| dynamic scope, or SBCL warnings?
+---------------

While you can perhaps turn off the warning [I think both SBCL
and CMUCL have some variable (not the same name?) you can set
that will suppress it], don't count on being able to turn off
the dynamic scope. In ANSI Common Lisp *all* global variables
are special, that is, have dynamic scope.

But you can fake it. Google for articles about using CL's
DEFINE-SYMBOL-MACRO (usually inside a macro called DEFLEXICAL
or DEFLEX) to define "global lexical variables" which can
then be shadowed by lexical bindings. Here's the simplest
version of it I've seen so far:

    > (defmacro deflex (var val &optional (doc nil docp))    
	(let ((backing-var (make-symbol (symbol-name var))))
	  `(progn
	     (defparameter ,backing-var ,val ,doc)
	     ,@(when docp `((setf (documentation ',var 'variable) ,doc)))
	     (define-symbol-macro ,var ,backing-var))))

    DEFLEX
    > (deflex foo 13)

    FOO
    > (defun foo () foo)

    FOO
    > (let ((foo 234))
	(list foo (foo)))

    (234 13)
    > 

Is that what you're looking for?


-Rob

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