gpt4 book ai didi

lisp - 未实习符号的奇怪行为

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:23 25 4
gpt4 key购买 nike

二进制文件是从包含以下定义的 .lisp 文件生成的:

 (in-package :xpto)

(defmacro defparam (name init.value)
`(progn
(defvar ,name ,init.value)
(eval-when (:compile-toplevel :load-toplevel :execute)
(export ',name "xpto"))))

(defparam myparam 10)

(defun getparam()
myparam)

所以,我想制作一个补丁文件来覆盖那个符号,因此,我想到了类似的东西:

;; Unintern the given symbol because otherwise we are not able to overwrite it
;; once it was set through defvar
(eval-when (:compile-toplevel :load-toplevel :execute)
(unintern 'xpto::myparam :xpto))

加载补丁文件后,奇怪的行为开始出现:

>  :pa xpto
> myparam
>> Error: Attempt to take the value of the unbound variable `myparam'

上面的输出是我所期待的,但是这个让我很困扰:

>  (getparam)
>> 10

我原以为没有返回任何值,因为该符号不再存在。
我如何删除/解除 myparam 符号的任何引用都会返回上面显示的错误消息?

p.s:在补丁文件中运行以下表达式对我来说不是一个有效的选项:

(setf myparam 20)

最佳答案

当您评估(defun getparam)时,它会记住符号myparam,并且每当您调用(getparam)时,它都会返回符号值. myparam uninterning 意味着你无法通过名称在包中找到符号,但符号对象仍然存在。

(defvar myparam 10)
(defun getparam() myparam)
#'getparam
==> #<FUNCTION GETPARAM NIL ... (BLOCK GETPARAM MYPARAM)>
(unintern 'myparam)
#'getparam
==> #<FUNCTION GETPARAM NIL ... (BLOCK GETPARAM #:MYPARAM)>
(getparam)
==> 10

请注意,该函数的第二个打印输出包含一个 uninterned 符号。

编辑

How can I remove/unintern myparam symbol in a way that any reference to it return the error message shown above ?

没有办法 - 除了重新定义 getparam

但是,您可以不同地定义getparam:

(defvar myparam 10)
(defun getparam()
(symbol-value (or (find-symbol "MYPARAM")
(error "Symbol MYPARAM does not exist"))))
(getparam)
==> 10
(unintern 'myparam)
(getparam)
*** - Symbol MYPARAM does not exist

关于lisp - 未实习符号的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17216681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com