special含义:创建变量的动态绑定。
let 创建lexical var.
fun parameter 创建lexical var .如果正好函数参数与动态变量同名,则转化为动态变量。
1、为了将let创建的lexical var转化为使用动态绑定,则需要声明 (declare (special var)) .
2、若已声明为动态绑定变量。那么在接下来的lexical 作用域内想要再使用动态绑定,则需要声明 (locally (declare (special var-list)) ,@deal-code-list)
测试代码如下:
;----------------------------------------------------------------------------------------------------------------------------
;动态变量名与函数参数同名
(defvar *p* nil)
(defun print-p ()
(format t "*p=~a~&" *p*))
(defun test ()
(print-p)
(let ((*p* 111))
(print-p))
(print-p))
(defun test-1 (*p*)
(print-p)
(let ((*p* 111))
;(declare *p* special)
(declare (inline test-1) (special *p*))
(print-p))
(print-p))
结果如下:
;----------------------------------------------------------------------------------------------------------------------------
;动态变量名不在函数参数中
(defun print-test ()
;(declare (special x y)) ;好像没有什么影响(format t "x=~a y=~a~%" x y)
(shiftf x y x));exchange x y
(defun test-3 ()
(declare (special x y))
(format t "x=~a y=~a~%" x y))
(defun test-2 ()
(let (( x 111) (y 222))
;(declare (inline print-test) (special x y)); (inline print-test) 好像没有什么影响
(declare (special x y))
(setf x 123)
(print-test)
(test-3)))
(defun test-4 (x y)
(declare (special x y))
(print-test)
(test-3)
(let ((x 1111) (y 2222))
(format t "x=~a y=~a~%" x y); let variable binds
(locally (declare (special x y)) ;special binds
(format t "x=~a y=~a~%" x y))
(print-test)))
结果如下: