Home > Smalltalk > Doing surgery on Smalltalk Objects

Doing surgery on Smalltalk Objects

UPDATE: for class variable reflection, see the end

I had a class with an instance variable that required no accessors, like this:

Object subclass: #MyObject
instanceVariableNames: 'instVarWithoutAccessors'
...

The instance variable was initialized to hold an OrderedCollection, but after working with the object for a while, I decided that it should be a Dictionary.

Now comes the problem…  I had objects floating around that already contained a non-empty OrderedCollection.  I didn’t want to add otherwise-not-needed accessors.  Luckily, because of Smalltalk’s awesome reflection capabilities, it was a breeze to reach into these objects and surgically change them.  For each object, I just evaluated:

| temp oc |
oc := self instVarNamed: 'instVarWithoutAccessors'.
temp := Dictionary new.
oc do: [ :d | temp at: d name put: d ].
self
    instVarNamed: 'instVarWithoutAccessors'
    put: temp.

And a quick inspector showed that the objects now contained a Dictionary that contained all the elements contained in the original OrderedCollection (note that I changed the class/var names in the above text to simplify the explanation):

UPDATE: to get at a class variable via reflection, try:

 MyClass classPool at: #ClassVarName
Categories: Smalltalk Tags:

windows 7 product key

windows 7 product key

windows 7 key generator

windows 7 key generator

free winrar download

free winrar download

winzip activation code

winzip activation code

free winrar

free winrar

winzip free download

winzip free download

winrar free download

winrar free download

winrar download free

winrar download free

winzip free download full version

winzip free download full version

windows 7 activation crack

windows7 activation crack

windows 7 crack

windows 7 crack

free winzip

free winzip
\n