Hi.. I'm kind a new learn to lisp, specially mcl.
I've got a problem with making an instance of a class.
I understand that usually we use something like
(setf an-instance (make-instance 'a-class))
but i'm trying to do here is getting the instance's name from the user and pass it
into the setf function, something like this :
(princ "Name :")
(setf user-name (read))
(setf user-name (make-instance users))
---------
How do I put the actual value of username into there? Because when executing that
function it will create an instance of users called user-name. (That's not what i
want)
can anyone out there help me??
Thanks in advance
--
***********************************
{} Irma Sumera
/^\ ·······@cs.rmit.edu.au
/ 3rd year Computer Science
/
\..
***********************************
·······@cs.rmit.edu.au (Irma) writes:
> (princ "Name :")
> (setf user-name (read))
>
> (setf user-name (make-instance users))
> ---------
> How do I put the actual value of username into there? Because when executing that
> function it will create an instance of users called user-name. (That's not what i
> want)
>
> can anyone out there help me??
Hmm, summer school in lisp ;-)?
Anyway, what you want is probably to put the name you (read) into a slot
of your 'users' object, whatever slots that might have. If your 'users'
class is defined with an 'initarg' specifier for that slot, say ':name',
then you can do something like this:
(setf user-name (read))
(setf user (make-instance 'users :name user-name))
But, as I indicated, you must have defined :name to be an initarg in
your DEFCLASS form before this will work.
--
(espen)
Irma wrote:
> I understand that usually we use something like
> (setf an-instance (make-instance 'a-class))
>
> but i'm trying to do here is getting the instance's name from the user and pass it
> into the setf function, something like this :
>
> (princ "Name :")
> (setf user-name (read))
>
> (setf user-name (make-instance users))
> ---------
I'm assuming that READ reads a symbol.
I find user-name confusing, so I'll call it variable-name.
Use SYMBOL-VALUE to set its value (that is, the value of the symbol which is
the value of VARIABLE-NAME).
> (setf variable-name (read))
foo
=> FOO
> (setf (symbol-value variable-name) (make-instance 'a-class))
=> #<a-class>
and now
> foo
=> #<a-class>
hth,
Michael
--
·······@cs.rmit.edu.au (Irma) writes:
> (setf user-name (make-instance users))
> ---------
> How do I put the actual value of username into there?
You can do this with (set user-name (make-instance users)). This
works because SET is a function, and functions evaluate all their
arguments. SETF is a "special form", which only evaluate some of its
arguments (in this case, user-name is not evaluated but the
make-instance is.
Finally note that what this _does_ is to set the symbol that you
(hopefully) READ to point to a new instance of users. The symbol
does't really "name" the user, since given a user object, you have no
way of looking up its name. The name should probably be a slot in the
user class.
--
Frode Vatvedt Fjeld