Search code examples
smalltalk

Returning messages in Smalltalk


I have a Dictionary of objects I have created in smalltalk, which I am iterating over by enumerating it based on the key/value pairs.

For value object in the dictionary, I am calling a method on that object. Based on certain conditions, I would like for this object to be able to add a new member to dictionary, and possibly delete another one.

I've been looking at the 'Perform' and 'Messages' facilities in Smalltalk, but I'm not sure if it is even possible to do what I'm trying to do - is it possible to return a message (or multiple messages), which another object can process and perform?

For example, could my method return 'removeKey: 19' and 'add object' at the same time?

I am using GNU Smalltalk, if it matters.


Solution

  • When you iterate over the collection, pass the collection as part of the argument:

    aCollection copy do: [:each | each doSomethingOn: aCollection]
    

    The copy ensures that the #doSomethingOn: can alter the original collection without messing up the iteration.