tags:

views:

41

answers:

2

Since Squeak is purely Object Oriented I'm fairly certain that you should be able to pass functions as parameters to other functions, but when I was researching on this I couldn't find any information about this. Is my intuition correct? And if so, how is it done and how do I invoke them afterwards?

A: 

You're confusing two different things. The only real "functions" you pass around in Smalltalk are blocks, which you pass just by writing a block as an argument (like you do with every ifTrue:). If you want to send a message to an object but have the message be determined dynamically, you can pass the message name as a symbol (e.g. #value) and send it to some object (e.g. with perform:). You don't pass instance methods themselves. Either pass a selector symbol or pass a block that sends a message to call the method.

Chuck
Suppose I pass a symbol. How do I "translate" it into a message sent to an object?
EpsilonVector
A: 

Longer answer.

To pass a piece of executable code to a method, use a block.

The method definition is

method: aBlock
    aBlock value

and you execute it as follows

object method: [ Transcript show: 'hello' ].

if you want to pass a parameter to the piece of code, use a block with an argument.

The method definition is

method: aBlock
    aBlock value: 'parameter'

and you execute it as follows

object method: [ :arg | Transcript show: arg ].

the same can be done with 2 or unlimited parameters, using the methods value:value: and valueWithArguments: of the block.

If you pass in a symbol, you can also use value: to execute it. A symbol is actually equivalent to a block of the form [ :arg | arg symbol ].

Adrian