Hi all,
I'm new to the language and to the REPL environment.
When I'm using Slime, and use C-c C-k to compile-and-load a file which contains constant definitions, Slime sometimes complains about constant redefinition. On the other hand, when I use C-c M-k to just compile the file, Slime sometimes just ignores the change in the file. I'm not quite sure if the latter phenomenon is just coincidence or it is so designated.
It makes me quite confused about the concept of compilation and loading. Does the compilation of a file makes the definitions inside the file accessible in the REPL? Or do I have to load the file? If compilation is enough, then what's the loading for? If load is mandatory, then why does compiling the defun makes the function accessible?
Thanks, E'Chao
Hi and welcome,
On Mar 1, 2007, at 5:03 AM, E'Chao Zhang wrote:
It makes me quite confused about the concept of compilation and loading. Does the compilation of a file makes the definitions inside the file accessible in the REPL?
No, it doesn't. The whole subject gets quite technical but compiling and loading are separate steps. The tricky thing is that both can happen in the same Lisp environment and the compiler may need to make some things active during the compilation process (e.g., macro definitions, constants, etc).
Or do I have to load the file?
Yes, you will.
If compilation is enough, then what's the loading for? If load is mandatory, then why does compiling the defun makes the function accessible?
If you're in the REPL and you "compile" a defun (or any s-expr), then you're really _evaluating_ it. Depending on the Lisp, this may produce a interpreted version of your defun or a compiled version (many Lisps nowawdays always compile and have no interpreter). This ends up being equivalent to compiling and loading but it, as I said, really evaluating...
HTH, -- Gary Warren King, metabang.com Cell: (413) 885 9127 Fax: (206) 338-4052 gwkkwg on Skype * garethsan on AIM
Gary King wrote:
Or do I have to load the file?
Yes, you will.
......
... but it, as I said, really evaluating...
If I'm not guessing wrong, Slime first compiles (and thus evaluates) the defconstant form, and when loading the fasl file, it again evaluates the same form, making the constant defined twice. When the constant is something like a string, the two defs will be considered different and thus cause a compilation error.
Is it actually a problem and how to avoid it?
Thanks again, E'Chao
On 3/1/07, E'Chao Zhang echaozh@gmail.com wrote:
Hi all,
I'm new to the language and to the REPL environment.
When I'm using Slime, and use C-c C-k to compile-and-load a file which contains constant definitions, Slime sometimes complains about constant redefinition. On the other hand, when I use C-c M-k to just compile the file, Slime sometimes just ignores the change in the file. I'm not quite sure if the latter phenomenon is just coincidence or it is so designated.
This isn't a Slime issue as much as in implementation one. You don't say what Lisp you're using, but I assume it's SBCL, as that matches the behavior you described.
The problem is that compiling a form *may* cause the things it defines to become defined, and in the case of constants and SBCL, it does. Then when you load the file, it gets redefined. Which is fine if the constant was 3, but not if it was '(1 2 3).
A common solution is to do something like:
(defmacro defconstant-equal (var value &optional doc) `(defconstant ,var (if (not (boundp ',var)) ,value (let ((old (symbol-value ',var)) (new ,value)) (if (equal old new) old (prog1 new (cerror "Redefine it anyway" "New value ~S for constant ~S is not equal to old value ~S" new ',var old))))) ,@(when doc (list doc))))
Then use defconstant-equal instead of defconstant. Or just use defparameter.
Thomas F. Burdick wrote:
This isn't a Slime issue as much as in implementation one. You don't say what Lisp you're using, but I assume it's SBCL, as that matches the behavior you described.
The problem is that compiling a form *may* cause the things it defines to become defined, and in the case of constants and SBCL, it does. Then when you load the file, it gets redefined. Which is fine if the constant was 3, but not if it was '(1 2 3).
A common solution is to do something like:
(defmacro defconstant-equal (var value &optional doc) `(defconstant ,var (if (not (boundp ',var)) ,value (let ((old (symbol-value ',var)) (new ,value)) (if (equal old new) old (prog1 new (cerror "Redefine it anyway" "New value ~S for constant ~S is not equal to old value ~S" new ',var old))))) ,@(when doc (list doc))))
Then use defconstant-equal instead of defconstant. Or just use defparameter.
Well, I didn't quite expect it to be a problem of Slime... but I thought it may have something to do with it... and it's an active list : )
Thanks for the great explanation, I should try it.