Hello all,
Is a class slot defined as :allocation :class accessible after
defclass'ing it, but before instantiating any objects of the class?
What I want to accomplish is opening a (logfile) stream and set the result
in the :class slot before make-instance is called of the class.
I would really appreciate a reply by email as well as posting the answer.
Thanks,
Leon.
In article <··················@biceps>,
Roos Van Raadshooven L.A. (Leon) <········@sparta.research.kpn.com> wrote:
...
>Is a class slot defined as :allocation :class accessible after
>defclass'ing it, but before instantiating any objects of the class?
I think you need an instance to utilize the slot accessing methods.
(at least all of the slot accessing methods I can recall).
However, it seems to be the case that you wish to initialize the
class slot to some value. I think :initform will do that
for you.
Here's some example code....
(defparameter *A-count* 0 )
(defvar *memo-file-open* (let ( (init? nil) )
#'(lambda ()
(incf *A-count* )
(if init?
init?
(setq init? "open the file")))))
(defclass A ()
( (c :accessor slot-class
:allocation :class
:initform (funcall *memo-file-open* ) ) ) )
If read the specs correctly, it should be the case that after the definition
of class A that the parameter *a-count* is 1. Which means the shared
slot is initialized along with the class definition. ( this is what happened
in the two implementations I tried the above in.)
The "memoized" closure is simply there to help illustrate when the initform was
evaluated (and to return the same value in case I was wrong about only
doing it once. ). I imagine you could do something like:
(defun init-recording-stream ()
.. open file and do whatever you wish to do .... )
(defclass A ()
( (c :accessor slot-c
:allocation :class
:initform (init-recording-stream ) ) ))
If your objective is to initialize the class slot after some "arbitrary"
computation and/or other initializations are done.... then this approach
may not work.
--
Lyman S. Taylor "Computers are too reliable to replace
(·····@cc.gatech.edu) humans effectively."
Commander Nathan Spring, "Starcops"