我有两个文件,一个 utils 文件,我在其中定义了很多常量,如下所示:
(defconstant peca-l0 (make-array (list 3 2) :initial-contents '((T T)(T nil)(T nil))))
(defconstant peca-l1 (make-array (list 2 3) :initial-contents '((T nil nil)(T T T))))
(defconstant peca-l2 (make-array (list 3 2) :initial-contents '((nil T)(nil T)(T T))))
(defconstant peca-l3 (make-array (list 2 3) :initial-contents '((T T T)(nil nil T))))
还有另一个我定义函数的文件。我像这样加载这个文件:
(load "utils.fas")
但是,每次我尝试使用它们时都会收到警告:
警告:在 ACCOES 中的第 121..174 行:PECA-L1 既未声明也未绑定(bind),它将被视为声明为 SPECIAL。
我该如何解决这个问题?
谢谢
最佳答案
引用自 HyperSpec 关于 defconstant
:
If a
defconstant
form appears as a top level form, the compiler must recognize that name names a constant variable. An implementation may choose to evaluate the value-form at compile time, load time, or both.
我自己对这个概念不太熟悉,但我猜你的实现只在编译时计算常量,也就是编译文件时。因此,在同一个文件中对这些常量的引用将起作用。但是当您随后加载已编译文件时,不会评估常量的定义,因此您无法在加载后访问它们。
添加 eval-when
应该解决这个问题:
(eval-when (:load-toplevel :compile-toplevel)
(defconstant +foo+ 'bar))
请注意,尽管使用 clisp 2.49
我无法重现您的问题。
关于lisp - 无法访问另一个文件中定义的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593850/