Search code examples
smalltalkpharo

Pharo big error?


i have a class at smalltalk in pharo. i create an instance in global dictionary which is smalltak,

 Smalltalk at:#asd put: myobject new.

then i write this code to workspace, mistakely.

 Smalltalk at:asd put: myobject new.

and this gives to me error, continuously, when a press a key from keyboard, i get error my object does not understand, is this byteArray, like that. how can i solve this issue? or how can i reset global dictionary?


Solution

  • Ok, this is a fun question.

    First of all I suggest you not to do things like that, because globals are usually used for storing classes in them.

    Now the cause of the problem is that you don't have to declare vars in workspace, it will create a new war for every name you use. By default new var is nil, so what you've done was putting a nil key in the dictionary

    To remove it execute this thing:

    Smalltalk globals removeKey: nil ifAbsent: [ ] 
    

    Now, if you want to have a unique instance of you class, I'd suggest you using a singleton pattern. This implies defining class instance variable (e.g. default) and a class method:

    default
      ^ default ifNil: [default := self new ].
    

    This will create new instance first time you run it, and then keep it and return it whenever you want it later.