Hi,
I need a function like this one:
(defun register-new ()
(flet ((get-name ()
"some-name"))
(macrolet ((register-name (name)
`(progn
(defclass ,name ()
((some-variable
:initarg :some-variable)))
(format t "~s~%" (slot-value
(make-instance
',name
:some-variable "value")
'some-variable)))))
(let ((name (get-name)))
(register-name name)
name))))
(register-new)
But I have a problem. As you can see the printed value and the
returned value differ. The goal is to be able to define a class with a
name that is dynamically generated as well as to to be able to
instantiate it.
Thanks in advance!
--
Kamen TOMOV
As Pascal noted, I would think eval would be appropriate here. However,
an alternative is ensure-class, which I believe is part of the MOP.
Cheers, david
On Wed, 10 May 2006 15:53:35 +0300, Kamen TOMOV <·····@evrocom.net> wrote:
>
>Hi,
>
>I need a function like this one:
>
>(defun register-new ()
> (flet ((get-name ()
> "some-name"))
> (macrolet ((register-name (name)
> `(progn
> (defclass ,name ()
> ((some-variable
> :initarg :some-variable)))
> (format t "~s~%" (slot-value
> (make-instance
> ',name
> :some-variable "value")
> 'some-variable)))))
> (let ((name (get-name)))
> (register-name name)
> name))))
>(register-new)
>
>But I have a problem. As you can see the printed value and the
>returned value differ. The goal is to be able to define a class with a
>name that is dynamically generated as well as to to be able to
>instantiate it.
>
You'll want to investigate using the MOP for this. The
MOP is not part of the standard, but a couple chapters
from _The Art of the Metaobject Protocol_ are provided
at:
http://www.lisp.org/mop/index.html
If you follow the Concepts link, scroll down
until you find Figure 1, which illustrates how
an implementation of defclass *could* be implemented
using the MOP, and then that should provide some
hints.
Also, you can macroexpand a sample defclass form
to see how your particular implementation actually
implements defclass.
I'd recommend downloading and getting familiar
with Closer to MOP:
http://common-lisp.net/project/closer/
--
Jack Unrue
On Wed, May 10 2006, Jack Unrue wrote:
> You'll want to investigate using the MOP for this.
> ..
Thank you very much for the idea and for the links. MOP is in my
readlist.
--
Kamen TOMOV
Kamen TOMOV wrote:
> Hi,
>
> I need a function like this one:
>
> (defun register-new ()
> (flet ((get-name ()
> "some-name"))
> (macrolet ((register-name (name)
> `(progn
> (defclass ,name ()
> ((some-variable
> :initarg :some-variable)))
> (format t "~s~%" (slot-value
> (make-instance
> ',name
> :some-variable "value")
> 'some-variable)))))
> (let ((name (get-name)))
> (register-name name)
> name))))
> (register-new)
>
> But I have a problem. As you can see the printed value and the
> returned value differ. The goal is to be able to define a class with a
> name that is dynamically generated as well as to to be able to
> instantiate it.
This is one of the rare cases where using eval can be a good idea:
(eval `(defclass ,name ...))
In other words, don't use a macro here but a function for register-name,
and then use eval inside that function.
Another way would be to use the CLOS MOP to programmatically create
classes, but using eval here is probably good enough.
Pascal
--
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
On Wed, May 10 2006, Pascal Costanza wrote:
> This is one of the rare cases where using eval can be a good idea:
>
> (eval `(defclass ,name ...))
>
> In other words, don't use a macro here but a function for
> register-name, and then use eval inside that function.
>
> Another way would be to use the CLOS MOP to programmatically create
> classes, but using eval here is probably good enough.
Hei, thank you very much for the great idea! I tried some other
approaches with eval before, but got tripped by variables defined in
lexical scopes and missing in dynamic. I just tried your idea - works
great. Many thanks.
--
Kamen TOMOV