home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / objectiv / 726 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  3.4 KB

  1. Path: sparky!uunet!casey!gaboon!seltd!lerman
  2. From: lerman@seltd.UUCP (Kenneth Lerman)
  3. Newsgroups: comp.lang.objective-c
  4. Subject: Re: why no class variables?
  5. Message-ID: <30@seltd.UUCP>
  6. Date: 23 Jan 93 20:09:02 GMT
  7. References: <1993Jan22.133837.13502@news.media.mit.edu>
  8. Reply-To: lerman@seltd.UUCP (Kenneth Lerman)
  9. Organization: Systems Essentials Limited
  10. Lines: 92
  11.  
  12. In article <1993Jan22.133837.13502@news.media.mit.edu> wave@media.mit.edu (Michael B. Johnson) writes:
  13. >Hi folks.  For a project I've been doing for some time now, I need to keep
  14. >some state info in the Class of a given object.  I'm annoyed by the fact
  15. >that I can't add instance variables to a Class object, and have to "simulate"
  16. >them with static variables.  Off the top of my head, I don't understand this
  17. >restriction.  Can someone out there convince me that this is a necessary 
  18. >and proper restriction?
  19.  
  20. I believe that so called "Class Variables" would be a useful addition
  21. to the Objective-C language.  These variables would be accessible by
  22. the methods of the class and inherited from the superclass in an
  23. analogous fashion to the way instance variables are inherited
  24.  
  25. That is, each class would have its own copy of each of the class
  26. variables it inherits (and each it defines).  Thus one could implement
  27. methods which keep track of how many instances of each class exist by
  28. the following:
  29.  
  30. // Note the single syntax change --
  31. // class and instance variables are specified as:
  32. // { classvars } : { instvars }
  33. //              OR to specify just instance variables
  34. // { instvars }
  35.  
  36. @implementation CountedObject:Object
  37. {
  38.     // instance variables go here
  39.     int instanceCount;
  40. } :
  41. {
  42.     // instance variables go here
  43. }
  44.  
  45. +new
  46. {
  47.     // note that class variables may be accessed from within a class
  48.     // method simply by referencing the name
  49.     instanceCount++;
  50.     return [super new];
  51. }
  52.  
  53. -free
  54. {
  55.     // note that class variables may be accessed from within an instance
  56.     // method simply by referencing the name
  57.     instanceCount--;
  58.     return [super free];
  59. }
  60.  
  61. -(int)howMany
  62. {
  63.     return instanceCount;
  64. }
  65.  
  66. -printHowMany
  67. {
  68.     printf("There are %d instances of class:%s\n",
  69.         [self howMany], isa->clsName);
  70.     return self;
  71. }
  72.  
  73. @end
  74.  
  75. >
  76. >For those who care, the situation for where I need them is that I have a set
  77. >of classes which, at run-time, start up companion processes on other machines.
  78. >The machines that could run a given class are queried when the class is
  79. >+initialize'ed, and then when an instance of that class is requested, that
  80. >list is queried to find the best host at that time to run the new instance
  81. >on.  I need to keep this list of possibleHosts somewhere, and it seems 
  82. >appropriate to make it a class instance variable, but as far as I can tell,
  83. >there's now way in ObjC to do this.  Pleae correct me if I'm wrong...
  84. >
  85.  
  86. Note that the semantics of the above proposal differ from the simple
  87. use of static variables in that every subclass has its own private
  88. copy of the class variables of each of its superclasses.  It is a pain
  89. in the neck to implement this functionality using other mechanisms.
  90.  
  91. >
  92. >-- 
  93. >
  94. >-->  Michael B. Johnson
  95. >-->  MIT Media Lab      --  Computer Graphics & Animation Group
  96. >-->  (617) 253-0663     --  wave@media-lab.media.mit.edu
  97.  
  98.  
  99. -- 
  100. Kenneth Lerman                  ...!uunet!casey!gaboon!seltd!lerman
  101. Systems Essentials Limited                            (203)426-4430
  102. 55 Main Street
  103. Newtown, CT 06470
  104.