Reputation:
I don't mean a class variable. I want a variable that can be used everywhere. Where should I define it? [in squeak]
Upvotes: 4
Views: 2728
Reputation: 9951
Squeak stores all class instances and other global variables in the SystemDictionary called "Smalltalk". You can define a global variable like this:
Smalltalk at: #variableName put: theValue.
Refering to the variable variableName
will return theValue
.
However, good Smalltalk style is to avoid global variables altogether.
Upvotes: 6
Reputation: 3964
One way is to make a singleton, as in this answer.
In general you make a class variable and accompanying class method to let some object become a globally accessible. See above mentioned singleton as an example. Such variable is then accesses from elsewhere:
global := MyClass myGlobalVar
To became also globally changeable, make mutator class method and call it like:
MyClass myGlobalVar: true
There are other ways too, but this one with class variables is portable around Smalltalk dialects, long term it is therefore the most safe way.
Upvotes: 4
Reputation: 10851
Well a class in smalltalk is globally available und you can change it whenever you like. Just create a class and add your altering code as class methods. You can then access your stuff by calling
MyVariable thisOrThat
MyVariable updateThisOrThat: aThisOrThat
Upvotes: 1