swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating Objects from the .scm file


From: Benedikt Stefansson
Subject: Re: Creating Objects from the .scm file
Date: Mon, 15 Nov 1999 11:11:31 -0700

Hi again,

I just wanted to clarify one thing, so as not to create any confusion
Obviously the Objective-C runtime can't 'create a class' - it can simply
return a reference to a class that has already been compiled. So if you set
antClass to NavyAnt and the class 'NavyAnt' doesn't exist, the program will
crash.

Also in my example the string 'name' would correspond to the string 'antClass'
in your .scm file. Corrected example:

ant   = nameToObject(antClass);
if(!ant)
{
   raiseEvent(InvalidOperation,"AntColony: Class %s not found\n",antClass);
}
ant   = [ant create: [self getZone]];

Benedikt

Benedikt Stefansson wrote:

> Hi Paul,
>
> Creating classes at runtime is one of the many great things that
> Objective-C supports. Iin the Swarm API you have the method -id
> nameToObject(const char *name) for this purpose. It returns a pointer to
> the class corresponding to the string name, you can then call the class to
> create an instance of the class.
>
> Hence  make sure that the antClass instance variable of the AntColony class
> is a string. Then implement a special method in AntColony which creates
> instances of the antClass like this:
>
> ant   = nameToObject(name);
>   if(!ant)
>     {
>       raiseEvent(InvalidOperation,"AntColony: Class %s not found\n",name);
>     }
>   ant   = [ant create: [self getZone]];
>
>   /* Set parameters in new object */
>   [ant set_Ivar1: val1];
>     ....
>   [ant set_IvarN: valN];
>
> (Substitute own Ivar names for Ivar1,...,IvarN). And you are off and
> running.
>
> Regards,
> Benedikt
>
> Pau Fernandez Duran wrote:
>
> > Hi all,
> >
> > I just would like to know if there is any way in which I could create an
> > object using a class object specified in a .scm file. As an example, we
> > could have:
> >
> > (list
> >  (cons 'colony
> >        (make-instance 'AntColony
> >                       #:antClass ArmyAnt      <--------- Right here !
> >                       ...
> >
> > This is useful if you want to switch between diferent classes of ants in
> > the same colony without having to compile each time. I was thinking of
> > using the string "ArmyAnt" and calling a function (which I don't know if
> > really exists) _get_class_from_String() or similar, but I haven't found
> > such a function (yet).
> >
> > Any hints??
> >
> > Many thankx in advance,
> >
> > ------------------------------------------------------
> > OOO Pau Fernandez Duran
> > OOO Complex Systems Research Group
> > OOO Departament de Física y Enginyeria Nuclear (FEN)
> > UPC
> >
> > Mòdul B4,   Campus Nord UPC  Tel.   +34-93-4017056
> > c/ Sor Eulalia d'Anzizu s/n  email: address@hidden
> > 08034 Barcelona
> > SPAIN
> > ------------------------------------------------------
> >
> >                   ==================================
> >    Swarm-Support is for discussion of the technical details of the day
> >    to day usage of Swarm.  For list administration needs (esp.
> >    [un]subscribing), please send a message to <address@hidden>
> >    with "help" in the body of the message.
>
>                   ==================================
>    Swarm-Support is for discussion of the technical details of the day
>    to day usage of Swarm.  For list administration needs (esp.
>    [un]subscribing), please send a message to <address@hidden>
>    with "help" in the body of the message.


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]