tags:

views:

106

answers:

3

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.

+5  A: 

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.

Randal Schwartz
+1  A: 

A Smalltalk method can't return multiple values, but it can return a Collection containing those values:

foo
  ^ Array with: 1 with: 2.

So you return a Collection with multiple methods, and just iterate over it, sending the messages in the Collection.

Frank Shearar
+1  A: 

The class Message can do what you want:

(Message selector: #raisedTo: argument: 2) sendTo: 3

That produces "9" when evaluated.

Note, adding or removing things from a collection while iterating over it generally is not a good idea. Try copying the collection first, iterating over the copy and modifying the original from within the block being used to iterate over the copy.

russ