Search code examples
smalltalksqueak

How to initiate global variable in Squeak


I don't mean a class variable. I want a variable that can be used everywhere. How should I initiate it?

I know one way is Smalltalk at: #variableName put: theValue. but I don't want to give it value as soon as I create it.


Solution

  • This should do the trick:

    Smalltalk at: #VariableName put: nil

    When you create a global variable in the Smalltalk dictionary, you are creating an Association which has a key and a value, so you can't get away without providing some sort of value.

    You can come back later and change the value with:

    Smalltalk at: #VariableName put: newValue

    Any compiled code that references the global variable will see the new value, because the compiled code references the Association.