Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition?
Thanks in advance. Uko |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Hi Uko,
On Wed, Mar 19, 2014 at 3:26 PM, Yuriy Tymchuk <[hidden email]> wrote: Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition? Class variables, as in classVariableNames: 'AClassVariable' are global variables in scope in all methods of the class and its subclass, instannce methods of the class and instance methods if the subclass. In the example, there is only one AClassVariable in the class.
Class instance variables are instance variables in the class object. The class object inherits from Behavior, ClassDescription and Class, and hence already has the superclass, methodDict, format, instanceVariables, organization, subclasses, name, classPool, sharedPools instance variables (details depending on dialect). The instanceVariableNames: on the class side add to these, but they are per-instance, and so the class and each of its subclasses has one of these instance variables and hence each class can have its own value for it, just as the class and each of its subclasses have their own method dictionaries. Further, the class instance variable is only in scope in class methods.
HTH, Eliot
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
In reply to this post by Uko2
A class variable is shared between all the subclasses. A class side instance variable is like instance variable, a slot at class side: every subclass (class side) will have its own slot.2014-03-19 23:26 GMT+01:00 Yuriy Tymchuk <[hidden email]>: Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition? |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
In reply to this post by Uko2
Yuriy Tymchuk wrote:
> Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition? > > Thanks in advance. > Uko > > instance-variable: A variable that holds the private state of an object. In Smalltalk these can only be accessed by instance-methods. class-variable: An attribute of a class that is shared by that class, its subclasses, and all instances of those classes. Can be accessed by both instance-methods and class-methods. class-instance-variable: An attribute of a class that is not shared outside that specific class. Only the definition is inherited by subclasses, not the value . Each subclass has its own private value. Accessible only by class-methods. You will understand it best by experimenting. Here is one I made for myself... ivCounter - instance variable counter CVCounter - class variable counter CIVCounter - class instance variable counter "----------" Object subclass: #TestCounter instanceVariableNames: 'ivCounter iName' classVariableNames: 'CVCounter' poolDictionaries: '' category: 'MyExamples' TestCounter class "accessed by clicking the <Class> button" instanceVariableNames: 'CIVCounter' TestCounter subclass: #TestSubCounter instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'MyExamples' "----------" TestCounter name: myInstanceName iName := myInstanceName. TestCounter >> next ivCounter ifNil: [ ivCounter := 0 ]. CVCounter ifNil: [ CVCounter := 0 ]. ivCounter := ivCounter + 1. CVCounter := CVCounter + 1. TestCounter >> printOn: aStream super printOn: aStream. aStream nextPutAll: ' ['. aStream print: iName. aStream nextPutAll: '] ( '. aStream nextPutAll: ' ivCounter='; print: ivCounter. aStream nextPutAll: ', CIVCounter=n/a'. aStream nextPutAll: ', CVCounter='; print: CVCounter. aStream nextPutAll: ' )'. "----------" TestCounter class >> next2 CIVCounter ifNil: [ CIVCounter := 0 ]. CVCounter ifNil: [ CVCounter := 0 ]. CIVCounter := CIVCounter + 1. CVCounter := CVCounter + 1. TestCounter class >> printOn: aStream super printOn: aStream. aStream nextPutAll: ' ( '. aStream nextPutAll: 'ivCounter=n/a'. aStream nextPutAll: ', CIVCounter='; print: CIVCounter. aStream nextPutAll: ', CVCounter='; print: CVCounter. aStream nextPutAll: ' )'. TestCounter class >> reset CVCounter := nil. CIVCounter := nil. "---------" Then from Workspace evaluate these... tc1 := TestCounter new. tc2 := TestCounter new. tsc1 := TestSubCounter new. tsc2 := TestSubCounter new. TestCounter reset. TestSubCounter reset. tc1 next. tc2 next. tc1 next. tc2 next. tsc1 next. tsc2 next. tsc1 next. tsc2 next. TestCounter next2. TestCounter next2. TestCounter next2. TestSubCounter next2. TestSubCounter next2. TestSubCounter next2. cheers -ben |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Thank you all for your replies.
I would say, that “Class Variable” name is a bit misleading, but now I know what is going on behind it. :) Cheers! Uko On 19 Mar 2014, at 23:53, Ben Coman <[hidden email]> wrote: > Yuriy Tymchuk wrote: >> Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition? >> >> Thanks in advance. >> Uko >> >> > instance-variable: A variable that holds the private state of an object. In Smalltalk these can only be accessed by instance-methods. > > class-variable: An attribute of a class that is shared by that class, its subclasses, and all instances of those classes. Can be accessed by both instance-methods and class-methods. > > class-instance-variable: An attribute of a class that is not shared outside that specific class. Only the definition is inherited by subclasses, not the value . Each subclass has its own private value. Accessible only by class-methods. > > > You will understand it best by experimenting. Here is one I made for myself... > > ivCounter - instance variable counter > CVCounter - class variable counter > CIVCounter - class instance variable counter > > "----------" > Object subclass: #TestCounter > instanceVariableNames: 'ivCounter iName' > classVariableNames: 'CVCounter' > poolDictionaries: '' > category: 'MyExamples' > > TestCounter class "accessed by clicking the <Class> button" > instanceVariableNames: 'CIVCounter' > > TestCounter subclass: #TestSubCounter > instanceVariableNames: '' > classVariableNames: '' > poolDictionaries: '' > category: 'MyExamples' > "----------" > TestCounter name: myInstanceName > iName := myInstanceName. > TestCounter >> next > ivCounter ifNil: [ ivCounter := 0 ]. > CVCounter ifNil: [ CVCounter := 0 ]. > ivCounter := ivCounter + 1. > CVCounter := CVCounter + 1. > TestCounter >> printOn: aStream > super printOn: aStream. > aStream nextPutAll: ' ['. aStream > print: iName. > aStream nextPutAll: '] ( '. aStream > nextPutAll: ' ivCounter='; > print: ivCounter. > aStream > nextPutAll: ', CIVCounter=n/a'. > aStream > nextPutAll: ', CVCounter='; > print: CVCounter. > aStream nextPutAll: ' )'. "----------" TestCounter class >> next2 > CIVCounter ifNil: [ CIVCounter := 0 ]. > CVCounter ifNil: [ CVCounter := 0 ]. > > CIVCounter := CIVCounter + 1. > CVCounter := CVCounter + 1. > TestCounter class >> printOn: aStream > super printOn: aStream. > aStream nextPutAll: ' ( '. aStream > nextPutAll: 'ivCounter=n/a'. > aStream > nextPutAll: ', CIVCounter='; > print: CIVCounter. > aStream > nextPutAll: ', CVCounter='; > print: CVCounter. > aStream nextPutAll: ' )'. TestCounter class >> reset > CVCounter := nil. > CIVCounter := nil. > > "---------" > Then from Workspace evaluate these... > > tc1 := TestCounter new. > tc2 := TestCounter new. > > tsc1 := TestSubCounter new. > tsc2 := TestSubCounter new. > > TestCounter reset. > TestSubCounter reset. > > tc1 next. tc2 next. > tc1 next. > tc2 next. > > tsc1 next. > tsc2 next. > tsc1 next. > tsc2 next. > > TestCounter next2. > TestCounter next2. > TestCounter next2. > > > TestSubCounter next2. > TestSubCounter next2. > TestSubCounter next2. > > cheers -ben > > > > > ... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
In reply to this post by Uko2
read the pharo by example book.
On 19 Mar 2014, at 23:26, Yuriy Tymchuk <[hidden email]> wrote: > Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition? > > Thanks in advance. > Uko |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
In reply to this post by Uko2
On 20 Mar 2014, at 08:32, Yuriy Tymchuk <[hidden email]> wrote: > Thank you all for your replies. > > I would say, that “Class Variable” name is a bit misleading, but now I know what is going on behind it. :) Yes I would like to rename them to sharedVariable > > Cheers! > Uko > > On 19 Mar 2014, at 23:53, Ben Coman <[hidden email]> wrote: > >> Yuriy Tymchuk wrote: >>> Can anybody tell me what is the difference between the class variable on the "instance side” definition and instance variable on the “class side” definition? >>> >>> Thanks in advance. >>> Uko >>> >>> >> instance-variable: A variable that holds the private state of an object. In Smalltalk these can only be accessed by instance-methods. >> >> class-variable: An attribute of a class that is shared by that class, its subclasses, and all instances of those classes. Can be accessed by both instance-methods and class-methods. >> >> class-instance-variable: An attribute of a class that is not shared outside that specific class. Only the definition is inherited by subclasses, not the value . Each subclass has its own private value. Accessible only by class-methods. >> >> >> You will understand it best by experimenting. Here is one I made for myself... >> >> ivCounter - instance variable counter >> CVCounter - class variable counter >> CIVCounter - class instance variable counter >> >> "----------" >> Object subclass: #TestCounter >> instanceVariableNames: 'ivCounter iName' >> classVariableNames: 'CVCounter' >> poolDictionaries: '' >> category: 'MyExamples' >> >> TestCounter class "accessed by clicking the <Class> button" >> instanceVariableNames: 'CIVCounter' >> >> TestCounter subclass: #TestSubCounter >> instanceVariableNames: '' >> classVariableNames: '' >> poolDictionaries: '' >> category: 'MyExamples' >> "----------" >> TestCounter name: myInstanceName >> iName := myInstanceName. >> TestCounter >> next >> ivCounter ifNil: [ ivCounter := 0 ]. >> CVCounter ifNil: [ CVCounter := 0 ]. >> ivCounter := ivCounter + 1. >> CVCounter := CVCounter + 1. >> TestCounter >> printOn: aStream >> super printOn: aStream. >> aStream nextPutAll: ' ['. aStream >> print: iName. >> aStream nextPutAll: '] ( '. aStream >> nextPutAll: ' ivCounter='; >> print: ivCounter. >> aStream >> nextPutAll: ', CIVCounter=n/a'. >> aStream >> nextPutAll: ', CVCounter='; >> print: CVCounter. >> aStream nextPutAll: ' )'. "----------" TestCounter class >> next2 >> CIVCounter ifNil: [ CIVCounter := 0 ]. >> CVCounter ifNil: [ CVCounter := 0 ]. >> >> CIVCounter := CIVCounter + 1. >> CVCounter := CVCounter + 1. >> TestCounter class >> printOn: aStream >> super printOn: aStream. >> aStream nextPutAll: ' ( '. aStream >> nextPutAll: 'ivCounter=n/a'. >> aStream >> nextPutAll: ', CIVCounter='; >> print: CIVCounter. >> aStream >> nextPutAll: ', CVCounter='; >> print: CVCounter. >> aStream nextPutAll: ' )'. TestCounter class >> reset >> CVCounter := nil. >> CIVCounter := nil. >> >> "---------" >> Then from Workspace evaluate these... >> >> tc1 := TestCounter new. >> tc2 := TestCounter new. >> >> tsc1 := TestSubCounter new. >> tsc2 := TestSubCounter new. >> >> TestCounter reset. >> TestSubCounter reset. >> >> tc1 next. tc2 next. >> tc1 next. >> tc2 next. >> >> tsc1 next. >> tsc2 next. >> tsc1 next. >> tsc2 next. >> >> TestCounter next2. >> TestCounter next2. >> TestCounter next2. >> >> >> TestSubCounter next2. >> TestSubCounter next2. >> TestSubCounter next2. >> >> cheers -ben >> >> >> >> >> > > ... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
2014-03-20 8:43 GMT+01:00 Pharo4Stef <[hidden email]>:
It's exactly a shared variable, but that does not describe the scope. It's a shared variable accessible in all methods of a class, its metaclass and their subclasses...
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
It’s an evil variable :)
On 20 Mar 2014, at 09:07, Nicolas Cellier <[hidden email]> wrote:
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Administrator
|
In reply to this post by Uko2
pharo-users@lists.pharo.org is a better place to ask questions like this, so that they do not get missed amongst the dev threads, and to give the devs' inboxes a break.
Cheers,
Sean |
Free forum by Nabble | Edit this page |