How to create a global variable in Squeak?

I do not mean a class variable. I want a variable that can be used everywhere. Where should I identify him? [squeak]

+4
source share
3 answers

Squeak stores all class instances and other global variables in a SystemDictionary called Smalltalk. You can define a global variable as follows:

Smalltalk at: #variableName put: theValue. 

A reference to the variable variableName will return theValue .

However, a good Smalltalk style is to avoid global variables altogether.

+6
source

One way is to make singleton, in this answer .

In general, you make a class variable and a companion class method so that an object becomes globally accessible. See the above singleton for an example. Such a variable is then accessed from other sources:

 global := MyClass myGlobalVar 

To become globally mutable, make a mutator class method and name it like this:

 MyClass myGlobalVar: true 

There are other ways, but this one with class variables carries over around Smalltalk dialects, so it is the most reliable way.

+4
source

Well, a class in smalltalk is available worldwide and you can change it whenever you want. Just create a class and add your change code as class methods. Then you can access your materials by calling

 MyVariable thisOrThat MyVariable updateThisOrThat: aThisOrThat 
+1
source

All Articles