Setting values using reflection

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

Setting values using reflection

tgiaccone
I've been playing with smalltalk casually now, for months and really like it. But I"m
clearly still a newb.

I've reached the point in a small project where I want to set the values on
my object based on the values that are in a dictionary. The dictionary is filled
with a set of keys that represent attributes on the object.  So for a simple example,
imagine I have an object which has accessors for 'title' and 'title:'.

I want to iterate across the set of keys, being certain that each key is a valid msg
to send to myOBject. The send the object the message with the value from the dictionary.

Now, I  understand that I can do:

myObject perform: 'title' asSymbol.

and use that as a way to message myObject with the selector 'title'

As I understand it this is equivalent to getting the value with:

myObject title

What I've looked for an been unable to find is how to set the value.

What is the equivalent to:

myOBject title: 'A new title'.

using perform?

Thank you for your help.


Tony
Reply | Threaded
Open this post in threaded view
|

Re: Setting values using reflection

Stéphane Ducasse
anObject perform: (mySymbol, ':' ) asSymbol with: aValue
stef

On Jul 18, 2011, at 9:59 PM, tgiaccone wrote:

> I've been playing with smalltalk casually now, for months and really like it.
> But I"m
> clearly still a newb.
>
> I've reached the point in a small project where I want to set the values on
> my object based on the values that are in a dictionary. The dictionary is
> filled
> with a set of keys that represent attributes on the object.  So for a simple
> example,
> imagine I have an object which has accessors for 'title' and 'title:'.
>
> I want to iterate across the set of keys, being certain that each key is a
> valid msg
> to send to myOBject. The send the object the message with the value from the
> dictionary.
>
> Now, I  understand that I can do:
>
> myObject perform: 'title' asSymbol.
>
> and use that as a way to message myObject with the selector 'title'
>
> As I understand it this is equivalent to getting the value with:
>
> myObject title
>
> What I've looked for an been unable to find is how to set the value.
>
> What is the equivalent to:
>
> myOBject title: 'A new title'.
>
> using perform?
>
> Thank you for your help.
>
>
> Tony
>
> --
> View this message in context: http://forum.world.st/Setting-values-using-reflection-tp3676289p3676289.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>


Reply | Threaded
Open this post in threaded view
|

Re: Setting values using reflection

Mariano Martinez Peck
As Stef said, you can use #perform:with:  or #perform:with:with:  or #perform:with:with:with:
or.....

#perform: selector withArguments: argArray

:)

On Mon, Jul 18, 2011 at 10:41 PM, Stéphane Ducasse <[hidden email]> wrote:
anObject perform: (mySymbol, ':' ) asSymbol with: aValue
stef

On Jul 18, 2011, at 9:59 PM, tgiaccone wrote:

> I've been playing with smalltalk casually now, for months and really like it.
> But I"m
> clearly still a newb.
>
> I've reached the point in a small project where I want to set the values on
> my object based on the values that are in a dictionary. The dictionary is
> filled
> with a set of keys that represent attributes on the object.  So for a simple
> example,
> imagine I have an object which has accessors for 'title' and 'title:'.
>
> I want to iterate across the set of keys, being certain that each key is a
> valid msg
> to send to myOBject. The send the object the message with the value from the
> dictionary.
>
> Now, I  understand that I can do:
>
> myObject perform: 'title' asSymbol.
>
> and use that as a way to message myObject with the selector 'title'
>
> As I understand it this is equivalent to getting the value with:
>
> myObject title
>
> What I've looked for an been unable to find is how to set the value.
>
> What is the equivalent to:
>
> myOBject title: 'A new title'.
>
> using perform?
>
> Thank you for your help.
>
>
> Tony
>
> --
> View this message in context: http://forum.world.st/Setting-values-using-reflection-tp3676289p3676289.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>





--
Mariano
http://marianopeck.wordpress.com

Reply | Threaded
Open this post in threaded view
|

Re: Setting values using reflection

tgiaccone
brilliant, thank you very much.

Tony

On Mon, Jul 18, 2011 at 4:47 PM, Mariano Martinez Peck <[hidden email]> wrote:
As Stef said, you can use #perform:with:  or #perform:with:with:  or #perform:with:with:with:
or.....

#perform: selector withArguments: argArray

:)


On Mon, Jul 18, 2011 at 10:41 PM, Stéphane Ducasse <[hidden email]> wrote:
anObject perform: (mySymbol, ':' ) asSymbol with: aValue
stef

On Jul 18, 2011, at 9:59 PM, tgiaccone wrote:

> I've been playing with smalltalk casually now, for months and really like it.
> But I"m
> clearly still a newb.
>
> I've reached the point in a small project where I want to set the values on
> my object based on the values that are in a dictionary. The dictionary is
> filled
> with a set of keys that represent attributes on the object.  So for a simple
> example,
> imagine I have an object which has accessors for 'title' and 'title:'.
>
> I want to iterate across the set of keys, being certain that each key is a
> valid msg
> to send to myOBject. The send the object the message with the value from the
> dictionary.
>
> Now, I  understand that I can do:
>
> myObject perform: 'title' asSymbol.
>
> and use that as a way to message myObject with the selector 'title'
>
> As I understand it this is equivalent to getting the value with:
>
> myObject title
>
> What I've looked for an been unable to find is how to set the value.
>
> What is the equivalent to:
>
> myOBject title: 'A new title'.
>
> using perform?
>
> Thank you for your help.
>
>
> Tony
>
> --
> View this message in context: http://forum.world.st/Setting-values-using-reflection-tp3676289p3676289.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>





--
Mariano
http://marianopeck.wordpress.com


Reply | Threaded
Open this post in threaded view
|

Re: Setting values using reflection

simondenier
In reply to this post by Stéphane Ducasse

On 18 juil. 2011, at 22:41, Stéphane Ducasse wrote:

> anObject perform: (mySymbol, ':' ) asSymbol with: aValue

A bit more "user-friendly", or less intention-revealing if you wish:

anObject perform: mySymbol asMutator with: aValue

#asMutator does the same thing as in the above snippet.


Also just from the subject I first thought you wanted to set instance variables so here it is for the record :)
#instVarAt:put:   and  #instVarNamed:put:  also in class Object


> stef
>
> On Jul 18, 2011, at 9:59 PM, tgiaccone wrote:
>
>> I've been playing with smalltalk casually now, for months and really like it.
>> But I"m
>> clearly still a newb.
>>
>> I've reached the point in a small project where I want to set the values on
>> my object based on the values that are in a dictionary. The dictionary is
>> filled
>> with a set of keys that represent attributes on the object.  So for a simple
>> example,
>> imagine I have an object which has accessors for 'title' and 'title:'.
>>
>> I want to iterate across the set of keys, being certain that each key is a
>> valid msg
>> to send to myOBject. The send the object the message with the value from the
>> dictionary.
>>
>> Now, I  understand that I can do:
>>
>> myObject perform: 'title' asSymbol.
>>
>> and use that as a way to message myObject with the selector 'title'
>>
>> As I understand it this is equivalent to getting the value with:
>>
>> myObject title
>>
>> What I've looked for an been unable to find is how to set the value.
>>
>> What is the equivalent to:
>>
>> myOBject title: 'A new title'.
>>
>> using perform?
>>
>> Thank you for your help.
>>
>>
>> Tony
>>
>> --
>> View this message in context: http://forum.world.st/Setting-values-using-reflection-tp3676289p3676289.html
>> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>>
>
>

--
Simon Denier