Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk delegation / storing message selectors

I am learning Squeak and I was wondering if there is a way to 'store' a message to be sent to an object.

For example I would like to be able to do the following :

Delegator>>performWith: arg
    |target method|
    target := MyObject new.
    method := #myMethod. "Not sure what goes here"
    target sendMessage: method withArgs: arg. "Or how this call would work"

An alternative approach would be to specify both the target and the method in a block. However it is possible to do using the above approach?

like image 275
ahjmorton Avatar asked Aug 10 '13 17:08

ahjmorton


2 Answers

Well, perhaps i misunderstood your question, but you nearly guessed the answer: Send this message to your "target":

perform: aSymbol with: anObject

or:

perform: aSymbol withArguments: anArrayOfArguments

In your example:

target perform: method with: arg

like image 61
MartinW Avatar answered Nov 04 '22 01:11

MartinW


You can also try using an instance of the MessageSend object.

msg := MessageSend receiver: target selector: #myMethod arguments: #(arg1 arg2).
msg value.  "sends the message to it's receiver"

MessageSend can be used as is. Squeak, Pharo, etc. use MessageSend as the base class for MorphicAlarm - which one can use to delay the execution of message until a certain time in the future.

Happy coding!

like image 20
parselmouth Avatar answered Nov 04 '22 01:11

parselmouth