Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Runtime class definition without eval?

1 view
Skip to first unread message

robert_...@yahoo.com

unread,
Jan 6, 1999, 3:00:00 AM1/6/99
to
Is this possible? Say I want my code to take the name `human' from the user
and create a new class named human. I could do

(eval (list 'defclass user-input inheritance slots-list))

which could then be

(eval (defclass human () ((name :accessor name))))

But there probably is a way to get the first arg of defclass to evaluate to a
form or symbol without using eval, right?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Lyman S. Taylor

unread,
Jan 6, 1999, 3:00:00 AM1/6/99
to
In article <770dr4$1hh$1...@nnrp1.dejanews.com>,

<robert_...@yahoo.com> wrote:
>Is this possible? Say I want my code to take the name `human' from the user
>and create a new class named human. I could do
>
>But there probably is a way to get the first arg of defclass to evaluate to a
>form or symbol without using eval, right?

Since DEFCLASS is a macro.... no. DEFCLASS doesn't evaluate the
first argument. So you will need to "eval" the whole statement.

By the way.... backquote is useful in this context.

USER(13): (eval `(defclass ,(read) () ((name :accessor name))) )
human
#<STANDARD-CLASS HUMAN>
USER(14): (describe 'human )
HUMAN is a SYMBOL.
It is unbound.
It is INTERNAL in the COMMON-LISP-USER package.
It names a class #<STANDARD-CLASS HUMAN>
USER(15):


P.S. If you lisp implementation has a directway of creating
classess then you wouldn't need eval. Something along he
lines of
(progn
.... pre creation magic ...
(make-instance 'standard-class ... some magic ... )
.... post creation magic ... )

There may be a MOP way of doing creating a new class... but I
can't remember at the moment. As much as I like to discourage
use of eval, it works in this context. ;-)
[ It isn't necessary, but it is the quick, portable solution. ]
--

Lyman S. Taylor "I'm a Doctor! Not a commando."
(ly...@cc.gatech.edu) The enhanced EMH Doctor in a ST:Voyager epidsode.

Erik Naggum

unread,
Jan 7, 1999, 3:00:00 AM1/7/99
to
* robert_...@yahoo.com

| Is this possible? Say I want my code to take the name `human' from the user
| and create a new class named human. I could do
|
| (eval (list 'defclass user-input inheritance slots-list))
|
| which could then be
|
| (eval (defclass human () ((name :accessor name))))
|
| But there probably is a way to get the first arg of defclass to evaluate
| to a form or symbol without using eval, right?

consider MACROEXPAND and see what DEFCLASS expands into. it should be
instructive.

#:Erik

Espen Vestre

unread,
Jan 7, 1999, 3:00:00 AM1/7/99
to
robert_...@yahoo.com writes:

> Is this possible? Say I want my code to take the name `human' from the user
> and create a new class named human. I could do

If you're using a lisp which implements the MOP, see the documentation
of ensure-class. Otherwise, follow Erik's advice and see what defclass
expands into (which probably will be internal to some system-defined
package, i.e. something you shouldn't be messing with without taking
appropriate care).

--

regards,
Espen Vestre -- Telenor Nextel AS -- Norway

0 new messages