Hi,
I'm reading "On To Smalltalk" and trying some of its examples. On Chapter 18 is about class variables, but Dolphin seem to have something else called classInstanceVariableNames. What's the difference between class variables and class instance variables? O:-) |
Class Variables are globals (or unique) for the entire hierarchy.
Animal (class variable BestFriend) Dog Cat ... The class variable "BestFriend" is the same for all classes. Dog setBestFriend: Dog new. Cat bestFriend is identical to Dog bestFriend (or other class of the hierarchy). The class variable is the same for all of them. Instances Class Variables are locals for each class. Animal (instance class variable bestFriend) Dog Cat Here each class has his unique bestFriend variable. Dog setBestFriend: Dog new. Cat setBestFriend: Cat new. Cat bestFriend is diffent to Dog bestFriend, because are different variables. Regards Bruno --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.801 / Virus Database: 544 - Release Date: 24/11/2004 |
Just to add a couple of extra bits to Bruno's answer.
ClassInstanceVariables are not just a Dolphin "thing", most (perhaps all?) modern Smalltalks implement it. They are not in the Blue Book but, from a quick check in my library, have been around since the mid 90's at least - FWIW I first saw them in ST/V for Win32. It should be noted that the scope of ClassVariables and ClassInstanceVariables is slightly different. - ClassVariables are directly accessible to both class and instance methods. - ClassInstanceVariables are only directly accessible to class methods. -- Ian Use the Reply-To address to contact me. Mail sent to the From address is ignored. |
In reply to this post by Bruno Buzzi Brasesco
On Sun, 28 Nov 2004 15:49:11 -0200, "Bruno" <[hidden email]>
wrote: >Instances Class Variables are locals for each class. >Animal (instance class variable bestFriend) > Dog > Cat Then what's the difference between them an regular instance variables? Why the need for both? O:-) |
On Sun, 28 Nov 2004 20:20:54 +0100,
Fernando <[hidden email]> wrote: > On Sun, 28 Nov 2004 15:49:11 -0200, "Bruno" <[hidden email]> > wrote: > > >>Instances Class Variables are locals for each class. >>Animal (instance class variable bestFriend) >> Dog >> Cat > > Then what's the difference between them an regular instance variables? > Why the need for both? O:-) I'm not sure if you absolutely need them, but: Vehicle CV: Wheels Car Bicycle would lead to serious problems when setting Wheels := 2 in Bicycle, unless you are James Bond. CIVs still allow you sharing *one* object between many instances, but they are firmly encapsulated in the owning class. Taking another point of view: CIV : Class :: IV : Object i.e. every class has its own CIVs just as every object has its own IVs. So actually globally available IV's are the special construct. HTH s. |
In reply to this post by Fernando Rodríguez
Fernando,
> Then what's the difference between them an regular instance variables? > Why the need for both? O:-) I'm going to regret this :-( OK. InstanceVariables and ClassInstanceVariables are really the same thing. When you create an instance of a class then you can create an InstanceVariable associated with the class. You can create any number of instances of a class and each one will have it's own individual InstanceVariables. Classes in Smalltalk are objects in their own right - each one is a Singleton instance of its metaclass. As a class is an instance of a parent class (metaclass) then it can have InstanceVariables. Because these variable are associated with the class object they have to be accessed in a different way to "normal" InstanceVariables and this is done by defining them as ClassInstanceVariables (InstanceVariables associated with a classes metaclass). As a class is a Singleton then there is only ever one ClassInstanceVariable associated with a class. So a ClassInstanceVariable is an InstanceVariable defined in the Metaclass of a Class and only accessible through class methods. That's about the fifth edit of that and I'm still not happy with it - but I've got to go out now :-) -- Ian Use the Reply-To address to contact me. Mail sent to the From address is ignored. |
Free forum by Nabble | Edit this page |