Search code examples
lispclisp

Can't access constants defined in another file


I have two files, a utils file where I have defined a lot of constants, like this:

(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))))

And another file where I'm defining the functions. I load this file like this:

(load "utils.fas")

However, every time I try to use them I have a warning:

WARNING: in ACCOES in lines 121..174: PECA-L1 is neither declared nor bound, it will be treated as if it were declared SPECIAL.

How can I fix this?

Thanks


Solution

  • Quoting from the HyperSpec about 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.

    I'm not all too familiar with the concept myself, but I'd guess that your implementation only evaluates the constants during compile time, that is when your file is compiled. As consequence references in that same file to these constants will work. But when you then load the compiled file, the constants' definitions aren't evaluated, thus you cannot access them after the load.

    Adding an eval-when should fix this:

    (eval-when (:load-toplevel :compile-toplevel)
      (defconstant +foo+ 'bar))
    

    Note though that using clisp 2.49 I cannot reproduce your issue.