From: Kamen TOMOV
Subject: defining a class dynamically
Date: 
Message-ID: <87psimkpm8.fsf@evrocom.net>
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

From: David E. Young
Subject: Re: defining a class dynamically
Date: 
Message-ID: <1147270266.147712.286150@i40g2000cwc.googlegroups.com>
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
From: Jack Unrue
Subject: Re: defining a class dynamically
Date: 
Message-ID: <v1r36213qe64h087165p53mf89odmndi58@4ax.com>
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
From: Kamen TOMOV
Subject: Re: defining a class dynamically
Date: 
Message-ID: <87vesenfkh.fsf@evrocom.net>
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
From: Pascal Costanza
Subject: Re: defining a class dynamically
Date: 
Message-ID: <4ce8s2F15nq23U1@individual.net>
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/
From: Kamen TOMOV
Subject: Re: defining a class dynamically
Date: 
Message-ID: <877j4uou9p.fsf@evrocom.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