singletonsmalltalkvisualworksdestruction

Visualworks Cincom Smalltalk Class Variable Destruction


How can we initialize a class variable in Visualworks Smalltalk and destroy it after its use? I want to know about ClassVariables. NOT ClassInstanceVariables.

I am implementing Singleton pattern and here is my code

MyClass class>> aClasMethod
         aClassVariable isNil ifTrue:[
               aClassVariable := 'I am a variable'.
         ]
         ^aClassVariable.

Once the variable is created, I am not able to destroy it. i.e Next time I run my code, I see that the class variable is retaining its previous value. How can I avoid this?

I tried this: MyClass allInstances do: [:inst | inst become: nil ]. But of no use.


Solution

  • The best way is simply to add a class method to set the class variable to nil and then call it whenever it's an appropriate time to clear it. I do this all the time with the Singleton pattern.