I just met an unusual situation in my common lisp code when I wanna test locally
and declare
:
(defvar test-out 2) ;; make a dynamic variable
;; function below just simply re-write from locally doc
(defun test (out)
(declare (special out))
(let ((out 1))
(print out) ;; => 1
(print (locally (declare (special out)) out)))) ;; => 2
;; when argument has same name as outside dynamic variable
(defun test1 (test-out)
(declare (special test-out))
(let ((test-out 1))
(print test-out) ;; => 1
(print (locally (declare (special test-out)) test-out)))) ;; => also 1
I know the right name of dynamic variable should be *test-out*
, but I thought it just for programmer to tell dynamic variable conveniently.
I am a bit confuse about test1
function, it looks like locally declare
do not point test-out
to dynamic variable outside.
Can anyone explain to me test1
function's behavior? Thanks
Update:
- I give a new dynamic variable
(defvar test-out-1 3)
, and call it like(test1 test-out-1)
, still get print out result1
and1
. - I change
test1
's argument name fromtest-out
totest-out1
, recompiletest1
, and problem disappears, print out results are1
and2
when I call(test1 test-out)
. - I change
(defvar test-out 2)
to(defvar test-out-1 2)
(change dynamic variable name). Then recompile whole file (no dynamic variable calledtest-out
this time, andtest1
argument's name istest-out
), problem disappears. - After 3, I call
(defvar test-out 2)
, and(test1 test-out)
. This time, it prints out right answers:1
and2
. - After 4, I recompile
test1
again, then run(test1 test-out)
, it prints out1
and1
again, problem appears again.
If I guess right, when test1
compiling, for some reason, its arguments' name connect to dynamic variable test-out
. That's why I receiving wrong result when I even call with different value, however, problem is solved by itself when I recompile test1
with different argument name or clean dynamic variable test-out
before recompile test.
If so, I still do not understand why compile function will effect by dynamic variable in the environment.