From: Paul Viney
Subject: Newbie compilation problem
Date: 
Message-ID: <121lh3f9tm1nia4@corp.supernews.com>
I have a file, test.lisp, containing the following:

(defclass point ()
  ((x :initarg :x :initform -1 :type integer :accessor point-x)
   (y :initarg :y :initform -1 :type integer :accessor point-y)))

(defconstant +default-position+
  (make-instance 'point))


When (using SBCL), I type (load "test.lisp"), then everything works fine.
However, if I type (compile-file "test.lisp"), then I get the error 
There is no class named POINT.

I've tried fiddling around with eval-when, but to no avail. Could anyone be
kind enough to give me a hand here and tell me what's going wrong?


Thanks,

Paul Viney

From: Eric Lavigne
Subject: Re: Newbie compilation problem
Date: 
Message-ID: <1142606305.200967.296020@p10g2000cwp.googlegroups.com>
> I have a file, test.lisp, containing the following:
>
> (defclass point ()
>   ((x :initarg :x :initform -1 :type integer :accessor point-x)
>    (y :initarg :y :initform -1 :type integer :accessor point-y)))
>
> (defconstant +default-position+
>   (make-instance 'point))
>
>
> When (using SBCL), I type (load "test.lisp"), then everything works fine.
> However, if I type (compile-file "test.lisp"), then I get the error
> There is no class named POINT.
>
> I've tried fiddling around with eval-when, but to no avail. Could anyone be
> kind enough to give me a hand here and tell me what's going wrong?
>

I repeated your experiment a few times in different ways. If I run SBCL
and immediately try to load the file then no problem. If I run SBCL and
immediately try to compile the file, then I get an error about an
undefined class, as you said. If I start SBCL, then load the file, then
compile it, I get a slightly different result: it complains about
redefining a constant. I respond to the complaint, saying go ahead and
redefine the constant, and now the compilation goes through fine.

So, taking these error messages at face value, it seems that SBCL was
trying to set the value of your constant before defining that class,
and that this problem didn't occur if the class happened to already be
defined (because the file had already been loaded).

I think there is something special about how constants are treated.
Their value is computed very early in compilation, so that it can truly
be a constant not just at run-time but also at compile-time.

As a test, try switching from defconstant to defvar. Now you will have
no problem whether or not you load the file first or just compile as
soon as you start SBCL.

Compiling is still one of the most confusing things about Lisp for me,
so take my interpretation as wild guess-work. I would just switch to
defvar, though.
From: Paul Viney
Subject: Re: Newbie compilation problem
Date: 
Message-ID: <121ljmo3ooahg09@corp.supernews.com>
Thanks for the feedback Eric, I'm changing to defparameter as Pascal
suggests.

Paul

> so take my interpretation as wild guess-work. I would just switch to
> defvar, though.
From: Pascal Bourguignon
Subject: Re: Newbie compilation problem
Date: 
Message-ID: <87mzfpcfaa.fsf@thalassa.informatimago.com>
Paul Viney <ยทยทยทยท@cablon.nl> writes:

> I have a file, test.lisp, containing the following:

(eval-when (:compile-toplevel :load-toplevel :execute)
> (defclass point ()
>   ((x :initarg :x :initform -1 :type integer :accessor point-x)
>    (y :initarg :y :initform -1 :type integer :accessor point-y)))
   )

> (defconstant +default-position+
>   (make-instance 'point))
>
>
> When (using SBCL), I type (load "test.lisp"), then everything works fine.
> However, if I type (compile-file "test.lisp"), then I get the error 
> There is no class named POINT.
>
> I've tried fiddling around with eval-when, but to no avail. Could anyone be
> kind enough to give me a hand here and tell me what's going wrong?

But  you'll still  get  errors,  or at  least  warnings, because  the
instance you're making at compilation time, won't be the same instance
made later when you'll load the compiled file.  

The morale is that you shouldn't use DEFCONSTANT on anything else than
a number, a character or a symbol.  Better use DEFPARAMETER here.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Small brave carnivores
Kill pine cones and mosquitoes
Fear vacuum cleaner
From: Paul Viney
Subject: Re: Newbie compilation problem
Date: 
Message-ID: <121ljkvearsnff6@corp.supernews.com>
Many thanks, I'll switch to defparameter.

Paul Viney