🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.objective-c
Article #359

Re: why no class variables?

#359
From: Kenneth Lerman
Date: Sat, 23 Jan 1993 20:09
92 lines
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
Technical Details
Message-ID: <30@seltd.UUCP>
Path: arc1-ca.pugleaf.net!sparky!uunet!casey!gaboon!seltd!lerman
References: <1993Jan22.133837.13502@news.media.mit.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!casey!gaboon!seltd!lerman
From: lerman@seltd.UUCP (Kenneth Lerman)
Newsgroups: comp.lang.objective-c
Subject: Re: why no class variables?
Message-ID: <30@seltd.UUCP>
Date: 23 Jan 93 20:09:02 GMT
References: <1993Jan22.133837.13502@news.media.mit.edu>
Reply-To: lerman@seltd.UUCP (Kenneth Lerman)
Organization: Systems Essentials Limited
Lines: 92