invoking slot (method) by its name

View: New views
5 Messages — Rating Filter:   Alert me  

invoking slot (method) by its name

by Milos Negovanovic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have a situation where I have available name of object's method slot
which I would like to invoke. What is the best way to do this? I would
like for the method to be invoked in the context of the object on which
method slot resides.

Something like

Test := Object clone do(
    a := method(s,
        s println
        self slotNames println
        self getSlot("b") call("bbb")
    )

    b := method(s,
        self slotNames println
        s println
    )

)

t := Test clone

t getSlot("a") call("aaa") # 1)

the_a := t getSlot("a")    # 2)
the_a("aaa")               # 2)


Problem with this example is that method slot 'a' is not invoked in
context of its object 't' ... 'self slotNames println' prints Lobby
contents.

Regards
--
Milos Negovanovic
milos.negovanovic@...

Re: invoking slot (method) by its name

by Friedrich Weber-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

I think the best way is to clone the method and set its scope to the
target context, so you get a kind of "bound method":

the_a := t getSlot("a") clone setScope(t)
the_a("aaa")

The method is cloned, otherwise further calls of that methods would have
`t` as the context.
It should also possible to use `performOn`, but that's not so nice.
Maybe I've missed an alternative, though - can anybody think of another? :)

Greetings,

Friedrich

Re: invoking slot (method) by its name

by Oscar Martinez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

About performOn: If called on a method it behaves as stated in the docs, but called on a block has no effect (the block is activated but not on the object passed). Why?

Regards,

Oscar.


Re: invoking slot (method) by its name

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009-07-12, at 2:46 AM, Milos Negovanovic wrote:

> I have a situation where I have available name of object's method slot
> which I would like to invoke. What is the best way to do this?

theObject perform(slotName)

Re: invoking slot (method) by its name

by Steve Dekorte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 2009-07-12, at 11:01 AM, Steve Dekorte wrote:

>> I have a situation where I have available name of object's method  
>> slot
>> which I would like to invoke. What is the best way to do this?
>
> theObject perform(slotName)

Sorry, please ignore this - I didn't read the rest of the question  
before responding. Friedrich's answer looks correct.