全域性特殊變數在各處都很特殊
因此這些變數將使用動態繫結。
(defparameter count 0)
;; All uses of count will refer to this one
(defun handle-number (number)
(incf count)
(format t "~&~d~%" number))
(dotimes (count 4)
;; count is shadowed, but still special
(handle-number count))
(format t "~&Calls: ~d~%" count)
==>
0
2
Calls: 0
為特殊變數提供不同的名稱以避免此問題:
(defparameter *count* 0)
(defun handle-number (number)
(incf *count*)
(format t "~&~d~%" number))
(dotimes (count 4)
(handle-number count))
(format t "~&Calls: ~d~%" *count*)
==>
0
1
2
3
Calls: 4
注 1:在某個範圍內不可能使全域性變數非特殊。沒有宣告來製作變數詞法。
注 2:可以使用 special
宣告在本地上下文中宣告一個特殊變數。如果該變數沒有全域性特殊宣告,則宣告僅在本地並且可以被遮蔽。
(defun bar ()
(declare (special a))
a) ; value of A is looked up from the dynamic binding
(defun foo ()
(let ((a 42)) ; <- this variable A is special and
; dynamically bound
(declare (special a))
(list (bar)
(let ((a 0)) ; <- this variable A is lexical
(bar)))))
> (foo)
(42 42)