I have a question, does CLOS have something like "this" in Java?
I'm trying to define a class that needs to be persisted. Since I'm
using CLISP I have to write the persistence code myself, and I designed
the class with an id slot to help identify it in the database. I need
to be able to assign an unique id to each newly created object, so I
need to access what's the largest id generated so far at the
instantiation of each instance. I want to set the :initform for the id
slot to a function that can do this automatically. I would like for the
data pertaining to the class to be encapsulated within the class, this
includes the max-id. Obviously this doesn't belong with any single
instance, but CLOS doesn't have static slots like Java. I can use class
allocated slot, but I would still need an instance to access it. i was
wondering if there is something like "this" from Java that I can use to
reference the instance being created from a lambda (like a special
variable or something that is in the lexical context).
Jeffery Zhang <····@cornell.edu> writes:
> I have a question, does CLOS have something like "this" in Java?
No, because methods are on generic functions, not in classes -- so the
concept doesn't make sense (but see below).
> I'm trying to define a class that needs to be persisted. Since I'm
> using CLISP I have to write the persistence code myself, and I
> designed the class with an id slot to help identify it in the
> database. I need to be able to assign an unique id to each newly
> created object, so I need to access what's the largest id generated so
> far at the instantiation of each instance. I want to set the :initform
> for the id slot to a function that can do this automatically. I would
> like for the data pertaining to the class to be encapsulated within
> the class, this includes the max-id. Obviously this doesn't belong
> with any single instance, but CLOS doesn't have static slots like
> Java. I can use class allocated slot, but I would still need an
> instance to access it. i was wondering if there is something like
> "this" from Java that I can use to reference the instance being
> created from a lambda (like a special variable or something that is in
> the lexical context).
You're not looking for 'this', you're looking for a constructor
method. And we have one, it's called initialize-instance. Eg:
(defmethod initialize-instance :after ((self some-class) &rest ignore)
(setf (id self) ...))
Jeffery Zhang <····@cornell.edu> writes:
> I have a question, does CLOS have something like "this" in Java?
>
> I'm trying to define a class that needs to be persisted. Since I'm
> using CLISP I have to write the persistence code myself, and I
> designed the class with an id slot to help identify it in the
> database. I need to be able to assign an unique id to each newly
> created object, so I need to access what's the largest id generated so
> far at the instantiation of each instance. I want to set the :initform
> for the id slot to a function that can do this automatically. I would
> like for the data pertaining to the class to be encapsulated within
> the class, this includes the max-id. Obviously this doesn't belong
> with any single instance, but CLOS doesn't have static slots like
> Java. I can use class allocated slot, but I would still need an
> instance to access it. i was wondering if there is something like
> "this" from Java that I can use to reference the instance being
> created from a lambda (like a special variable or something that is in
> the lexical context).
You can use a closure:
(let ((counter 0))
(defclass a ()
((id :initform (incf counter) :reader oid)
;; ...
)))
then counter won't be accessible outside of the initform of the id
slot of the class a.
(mapcar (function oid)
(list (make-instance 'a) (make-instance 'a) (make-instance 'a)))
--> (1 2 3)
--
__Pascal Bourguignon__ http://www.informatimago.com/
In a World without Walls and Fences,
who needs Windows and Gates?