Hi!
Let's suppose I have a class like this:
(defclass test ()
((slot1 :accessor slot1 :allocation :class)
(slot2 :accessor slot2)))
Obviously, to access slot2, I need to have an instance of a class.
But what about the slot1? Can I access it without instantiating an
object? Like, for example, in Java? Obviously, it's very simple to
call make-instance, but the problem is that this code will be run
many times a second, so it will produce lots of "garbage" to be
coolected, without and useful use.
Yours
Miguel Arroz
In article <·················@mystique.esoterica.pt>,
Miguel Arroz <·····@guiamac.com> wrote:
> Hi!
>
> Let's suppose I have a class like this:
>
> (defclass test ()
> ((slot1 :accessor slot1 :allocation :class)
> (slot2 :accessor slot2)))
>
> Obviously, to access slot2, I need to have an instance of a class.
> But what about the slot1? Can I access it without instantiating an
> object? Like, for example, in Java? Obviously, it's very simple to
> call make-instance, but the problem is that this code will be run
> many times a second, so it will produce lots of "garbage" to be
> coolected, without and useful use.
This has got to be the most FAQ about CLOS, and I'll bet a google search
for "allocation class" would probably have found it.
Standard CL doesn't provide a way, but most implementations have the MOP
function CLASS-PROTOTYPE, which returns a prototype instance of a class
that can be used for this.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Hi!
Thanks for the help. After some reading, I think I will do it in a
more LISP-way. The idea was to have an integer that would increase by
1 every time a function is called (somewhat like an auto-increment
field in a DB). I'll do it like this:
(let ((value 0))
(defun get-value ()
(incf value)
value))
Much more elegant, I guess! :-)
Yours
Miguel Arroz
From: Edi Weitz
Subject: Re: Accessing class methods and slots without instantiating
Date:
Message-ID: <87oelfgnpy.fsf@bird.agharta.de>
On Fri, 13 Aug 2004 13:32:48 GMT, Miguel Arroz <·····@guiamac.com> wrote:
> I'll do it like this:
>
> (let ((value 0))
> (defun get-value ()
> (incf value)
> value))
>
> Much more elegant, I guess! :-)
Yes. You can even get rid of the last line:
(let ((value 0))
(defun get-value ()
(incf value)))
Edi.
--
"Lisp doesn't look any deader than usual to me."
(David Thornley, reply to a question older than most languages)
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Raistlin Magere
Subject: Re: Accessing class methods and slots without instantiating
Date:
Message-ID: <cfim7c$m83$1@news.ox.ac.uk>
"Miguel Arroz" <·····@guiamac.com> wrote in message
······················@mystique.esoterica.pt...
> Hi!
>
> Thanks for the help. After some reading, I think I will do it in a
> more LISP-way. The idea was to have an integer that would increase by
> 1 every time a function is called (somewhat like an auto-increment
> field in a DB). I'll do it like this:
>
> (let ((value 0))
> (defun get-value ()
> (incf value)
> value))
>
> Much more elegant, I guess! :-)
>
You might want to also include a "reset-value" (or set-value) function
(let ((value 0))
(defun get-value () (incf value))
(defun reset-value (&optional new-value)
(if (numberp new-value) (setf value new-value) (setf value 0))))
From: Edi Weitz
Subject: Re: Accessing class methods and slots without instantiating
Date:
Message-ID: <87pt5vjm8i.fsf@bird.agharta.de>
On Fri, 13 Aug 2004 11:31:12 GMT, Miguel Arroz <·····@guiamac.com> wrote:
> Let's suppose I have a class like this:
>
> (defclass test ()
> ((slot1 :accessor slot1 :allocation :class)
> (slot2 :accessor slot2)))
>
> Obviously, to access slot2, I need to have an instance of a class.
> But what about the slot1? Can I access it without instantiating an
> object? Like, for example, in Java? Obviously, it's very simple to
> call make-instance, but the problem is that this code will be run
> many times a second, so it will produce lots of "garbage" to be
> coolected, without and useful use.
<http://groups.google.com/groups?threadm=877jz1wsnm.fsf%40ortler.iwr.uni-heidelberg.de>
Edi.
--
"Lisp doesn't look any deader than usual to me."
(David Thornley, reply to a question older than most languages)
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Miguel Arroz wrote:
> Hi!
>
> Let's suppose I have a class like this:
>
> (defclass test ()
> ((slot1 :accessor slot1 :allocation :class)
> (slot2 :accessor slot2)))
>
> Obviously, to access slot2, I need to have an instance of a class.
> But what about the slot1? Can I access it without instantiating an
> object?
First of all, think about whether you actually need the class-allocated
slot. In "Java-like" languages, static variables are usually just used
for global data. If you need global data, use defvar or defparameter,
that should usually do the job.
Second, the CLOS MOP provides a function CLASS-PROTOTYPE that you can
use for these purposes. It yields an uninitialized instance of a
specified class, and its purpose is actually to serve in method
specialization. So the code looks like this:
(slot1 (class-prototype 'test))
If your Common Lisp implementation doesn't provide a MOP you can
simulate the behavior of CLASS-PROTOYTPE via ALLOCATE-INSTANCE.
ALLOCATE-INSTANCE creates an instance of a class without initializing
it. Here is a rough implementation of CLASS-PROTOTYPE:
(defvar *prototypes* (make-hash-table))
(defgeneric class-prototype (class)
(:method ((class symbol))
(class-prototype (find-class class)))
(:method ((class class))
(let ((prototype (gethash class *prototypes*)))
(if prototype prototype
(setf (gethash class *prototype*)
(allocate-instance class))))))
Pascal
--
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."