Itâs an evil variable :) On 20 Mar 2014, at 09:07, Nicolas Cellier <nicolas.cellier.aka.nice@gmail.com> wrote:
2014-03-20 8:43 GMT+01:00 Pharo4Stef <pharo4Stef@free.fr>:
On 20 Mar 2014, at 08:32, Yuriy Tymchuk <yuriy.tymchuk@me.com> 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
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...
Cheers! Uko
On 19 Mar 2014, at 23:53, Ben Coman <btc@openinworld.com> 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