Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
453 posts
|
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? thanks! ----
peace, sergio photographer, journalist, visionary |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
281 posts
|
On 22 mai 2014, at 19:46, sergio_101 <[hidden email]> wrote:
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. |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
2446 posts
|
In reply to this post by sergio_101
Hi! On Thu, May 22, 2014 at 7:46 PM, sergio_101 <[hidden email]> wrote:
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
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).
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Administrator
5777 posts
|
In reply to this post by camille teruel
"instance perform: filterMessage asSymbol" ;)
Cheers,
Sean |
Free forum by Nabble | Edit this page |