Calling an arbitrary method

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Calling an arbitrary method

sergio_101
In many cases, an object might have a method (filter, selector, etc) attached to it so that the object can run the correct method..

for instance, an object might have a filterMethod variable with a value 'filterByArtist'..

in ruby, i would do something like:

instance.send(filter_method)

how would i do that in pharo, and is this a bad idea?

Reply | Threaded
Open this post in threaded view
|

Re: Calling an arbitrary method

camille teruel

On 22 mai 2014, at 19:46, sergio_101 <[hidden email]> wrote:

In many cases, an object might have a method (filter, selector, etc) attached to it so that the object can run the correct method..

for instance, an object might have a filterMethod variable with a value 'filterByArtist'..

in ruby, i would do something like:

instance.send(filter_method)

how would i do that in pharo, and is this a bad idea?

"instance perform: filterMethod asSymbol"
If filterMethod is already a symbol you can omit asSymbol. 
It is slower than regular message send but it's also the safest reflective operation since it's the only one that doesn't break object encapsulation.

Reply | Threaded
Open this post in threaded view
|

Re: Calling an arbitrary method

Guillermo Polito
In reply to this post by sergio_101
Hi!

On Thu, May 22, 2014 at 7:46 PM, sergio_101 <[hidden email]> wrote:
In many cases, an object might have a method (filter, selector, etc) attached to it so that the object can run the correct method..

for instance, an object might have a filterMethod variable with a value 'filterByArtist'..

in ruby, i would do something like:

instance.send(filter_method)

how would i do that in pharo,

Selectors (method signatures) in Pharo are represented with Symbols, not Strings. In order to send a message from a given symbol you can do:

object perform: #symbol

or

object perform: #symbol: with: anArgument

or

object perform: #symbol: withArguments: anArrayOfArguments

for example

2 perform: #even.
2 perform: #+ with: 3

 
and is this a bad idea?

It usually depends on what you want to achieve... You can also reach the same result having two filter objects:

NullFilter>>filter: aCollection

 ^ aCollection

ByArtistFilter>>filter: aCollection

  ^ aCollection select: [ :each | each artist = myArtist ]

And you can even think about a third solution with blocks describing the filters.

Making them objects to me gives you more power. They are objects, you can manipulate them, compose them, etc.
Blocks can capture variables from the scope, but they are a bit obscure (as symbols are too).
 

Reply | Threaded
Open this post in threaded view
|

Re: Calling an arbitrary method

Sean P. DeNigris
Administrator
In reply to this post by camille teruel
camille teruel wrote
"instance perform: filterMethod asSymbol"
"instance perform: filterMessage asSymbol" ;)
Cheers,
Sean