In addition to the explanations in the other posting, consider this
variation of your example:
;; no DEFVAR of B
> (setf b 'g) ;give it a global value but don't make it dynamic everywhere
G
> (defun foo (a) (cons a b))
;; maybe a warning about assuming B special
FOO
> (defun lex-bar (b) (foo 'lex))
LEX-BAR
> (defun dyn-bar (b)
(declare (special b)) ;DECLAIM is usually top level only
(foo 'dyn))
DYN-BAR
> (lex-bar 'val)
(LEX . G) ;LEX-BAR binds its parameter lexically so FOO doesn't see that
> (dyn-bar 'val)
(DYN . VAL) ;FOO sees the binding of B made by DYN-BAR
In the above example, B is not globally declared (proclaimed) special
(which it would have been with DEFVAR), but just in specific places.
Disclaimer: I have not actually run this example (sorry, too lazy),
please run it!
> I thought that (DECLAIM (SPECIAL B)) extends the dynamic environment
> with the value of B from the lexical environment.
(...)
DECLAIM does something else, but I won't go into it as I think you
were thinking about DECLARE here. (After you have things about
declarations clear, read the description of DECLAIM.)
Now (DECLARE (SPECIAL B)) means: within the scope of this declaration
treat all references to B as references to a dynamic variable, ignoring
any lexical bindings that might otherwise be in force. `The scope of
this declaration' is the form in which this declaration appears including
any nested forms *but* excluding nested forms like LET and DO that bind
B. (Well, this is just a rough description; consult the literature for
the Real Thing.)
If you want (I can't think of a reason) to `extend the dynamic
environment with the value of B from the lexical environment' you
can do e.g.
(let ((x b))
(declare (special x))
...)
or
(defvar x 'init "my starless and bible black global variable")
...
(let ((x b)) ...)
---assuming that B is a lexical variable in the scope that surrounds
this LET---and for the duration of the execution of the LET, anywhere
in the program text (i.e. not just in the textual body of the LET),
the binding for X in the dynamic environment (until changed) would
have B's value. Now if you rename X as B in the above example, you
get a lexical B and a dynamic B. I hope you are not too confused.
Good luck, and keep asking questions until you are sure you get it,
Vassil.
P.S. Beware of analogies with other programming languages like C:
some things about variables are similar and some are very different.
Vassil Nikolov <vniko...@poboxes.com> www.poboxes.com/vnikolov
(You may want to cc your posting to me if I _have_ to see it.)
LEGEMANVALEMFVTVTVM (Ancient Roman programmers' adage.)
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own