Thread View: comp.lang.objective-c
3 messages
3 total messages
Started by Michael B. Johnson
Fri, 22 Jan 1993 13:38
why no class variables?
Author: Michael B. Johnson
Date: Fri, 22 Jan 1993 13:38
Date: Fri, 22 Jan 1993 13:38
22 lines
1554 bytes
1554 bytes
Hi folks. For a project I've been doing for some time now, I need to keep some state info in the Class of a given object. I'm annoyed by the fact that I can't add instance variables to a Class object, and have to "simulate" them with static variables. Off the top of my head, I don't understand this restriction. Can someone out there convince me that this is a necessary and proper restriction? For those who care, the situation for where I need them is that I have a set of classes which, at run-time, start up companion processes on other machines. The machines that could run a given class are queried when the class is +initialize'ed, and then when an instance of that class is requested, that list is queried to find the best host at that time to run the new instance on. I need to keep this list of possibleHosts somewhere, and it seems appropriate to make it a class instance variable, but as far as I can tell, there's now way in ObjC to do this. Pleae correct me if I'm wrong... -- --> Michael B. Johnson --> MIT Media Lab -- Computer Graphics & Animation Group --> (617) 253-0663 -- wave@media-lab.media.mit.edu
Re: why no class variables?
Author: Kenneth Lerman
Date: Sat, 23 Jan 1993 20:09
Date: Sat, 23 Jan 1993 20:09
92 lines
3536 bytes
3536 bytes
In article <1993Jan22.133837.13502@news.media.mit.edu> wave@media.mit.edu (Michael B. Johnson) writes:
>Hi folks. For a project I've been doing for some time now, I need to keep
>some state info in the Class of a given object. I'm annoyed by the fact
>that I can't add instance variables to a Class object, and have to "simulate"
>them with static variables. Off the top of my head, I don't understand this
>restriction. Can someone out there convince me that this is a necessary
>and proper restriction?
I believe that so called "Class Variables" would be a useful addition
to the Objective-C language. These variables would be accessible by
the methods of the class and inherited from the superclass in an
analogous fashion to the way instance variables are inherited
That is, each class would have its own copy of each of the class
variables it inherits (and each it defines). Thus one could implement
methods which keep track of how many instances of each class exist by
the following:
// Note the single syntax change --
// class and instance variables are specified as:
// { classvars } : { instvars }
// OR to specify just instance variables
// { instvars }
@implementation CountedObject:Object
{
// instance variables go here
int instanceCount;
} :
{
// instance variables go here
}
+new
{
// note that class variables may be accessed from within a class
// method simply by referencing the name
instanceCount++;
return [super new];
}
-free
{
// note that class variables may be accessed from within an instance
// method simply by referencing the name
instanceCount--;
return [super free];
}
-(int)howMany
{
return instanceCount;
}
-printHowMany
{
printf("There are %d instances of class:%s\n",
[self howMany], isa->clsName);
return self;
}
@end
>
>For those who care, the situation for where I need them is that I have a set
>of classes which, at run-time, start up companion processes on other machines.
>The machines that could run a given class are queried when the class is
>+initialize'ed, and then when an instance of that class is requested, that
>list is queried to find the best host at that time to run the new instance
>on. I need to keep this list of possibleHosts somewhere, and it seems
>appropriate to make it a class instance variable, but as far as I can tell,
>there's now way in ObjC to do this. Pleae correct me if I'm wrong...
>
Note that the semantics of the above proposal differ from the simple
use of static variables in that every subclass has its own private
copy of the class variables of each of its superclasses. It is a pain
in the neck to implement this functionality using other mechanisms.
>
>--
>
>--> Michael B. Johnson
>--> MIT Media Lab -- Computer Graphics & Animation Group
>--> (617) 253-0663 -- wave@media-lab.media.mit.edu
--
Kenneth Lerman ...!uunet!casey!gaboon!seltd!lerman
Systems Essentials Limited (203)426-4430
55 Main Street
Newtown, CT 06470
Re: why no class variables?
Author: Jack Gidding
Date: Mon, 25 Jan 1993 15:26
Date: Mon, 25 Jan 1993 15:26
35 lines
1756 bytes
1756 bytes
In article <1993Jan22.133837.13502@news.media.mit.edu> wave@media.mit.edu (Michael B. Johnson) writes: > Hi folks. For a project I've been doing for some time now, I need to keep > some state info in the Class of a given object. I'm annoyed by the fact > that I can't add instance variables to a Class object, and have to "simulate" > them with static variables. Off the top of my head, I don't understand this > restriction. Can someone out there convince me that this is a necessary > and proper restriction? > I wholeheartedly agree! I think that these are necessary as well. Class variables are important for doing things exactly as you mentioned. I have the same problem. I set up some initialization for a class when the first one is created. By faking the class variables with static variables means that I have to restrict my subclassing. Grrr. For me, I need the static variables for time optimization. I look up function references and store them. Its a long story, but its a necessary optimization in our real-time system. --- Jack Gidding Sr. Development Engineer Reuters 1400 Kensington Road Oak Brook,IL 60521 jack@richsun.cpg.trs.reuter.com NeXT mail welcome! ----------------------------------------------------------- "Credit is a euphemism for debt." - Billy McKenzie
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads