From: Edi W. <ed...@ag...> - 2002-12-01 18:18:32
|
Hi! I have a file foo.lisp with the following contents: (defun foo (n) (1+ n)) (defconstant +bar+ (foo 10)) If I try to compile it, SBCL (0.7.10 on Linux) complains: * (compile-file "foo") ; compiling file "/tmp/foo.lisp" (written 01 DEC 2002 07:02:20 PM): ; recognizing DEFUN FOO ; compiling top level form: ; compiling DEFUN FOO: ; compiling top level form: debugger invoked on condition of type UNDEFINED-FUNCTION: The function FOO is undefined. OK, that's what I expected. The CLHS says about DEFCONSTANT that "users must ensure that the initial-value can be evaluated at compile time". OK, let's try it again, foo.lisp now is: (eval-when (:compile-toplevel :execute :load-toplevel) (defun foo (n) (1+ n))) (defconstant +bar+ (foo 10)) Which basically works... * (compile-file "foo") ; compiling file "/tmp/foo.lisp" (written 01 DEC 2002 07:03:55 PM): ; recognizing DEFUN FOO ; compiling top level form: ; compiling DEFUN FOO: ; compiling top level form: ; compiling top level form: ; /tmp/foo.fasl written ; compilation finished in 0:00:00 #P"/tmp/foo.fasl" NIL NIL * (load "foo") STYLE-WARNING: redefining FOO in DEFUN T ...except for the strange STYLE-WARNING. However, as soon as FOO gets a little more complicated, like, say, so... (eval-when (:compile-toplevel :execute :load-toplevel) (defun foo (n c) (make-string n :initial-element c))) (defconstant +bar+ (foo 10 #\a)) ...it won't work: * (compile-file "foo") ; compiling file "/tmp/foo.lisp" (written 01 DEC 2002 07:04:44 PM): ; recognizing DEFUN FOO ; compiling top level form: ; compiling DEFUN FOO: ; compiling top level form: ; compiling top level form: ; /tmp/foo.fasl written ; compilation finished in 0:00:00 #P"/tmp/foo.fasl" NIL NIL * (load "foo") STYLE-WARNING: redefining FOO in DEFUN debugger invoked on condition of type SIMPLE-ERROR: The constant +BAR+ is being redefined. Why is that? From my limited understanding of the compilation process I guess that there's something left over from the compilation environment which is responsible for the STYLE-WARNING as well as for the error shown. Is this correct behaviour? And if so, how am I supposed to deal with it? Wrap all DEFCONSTANTs into something like (unless (boundp '+bar+) (defconstant +bar+ ...)) or what? Thanks for your help, Edi. PS: All three examples above were executed in a freshly started image. AllegroCL, LispWorks, CMUCL, and CLISP don't show this behaviour. |