Bert Freudenberg | 3 Dec 2009 13:05
Picon
Gravatar

Re: Referencing a variable?

On 03.12.2009, at 12:59, Amir Ansari wrote:
> 
> But I'd like to reuse the code as much as possible, so I'd prefer to do something like this:
> 
> element := 'title'.
> choice := myLibrary collect: [:each | each element].

element := #title.
choice := myLibrary collect: [:each | each perform: element].

Note that this sends the "title" message. There is no way to directly access an instance variable from
outside the object. Objects in Smalltalk are true objects, not data structures. The code above only works
if you implemented a "title" method that returns the instance variable.

- Bert -
Amir Ansari | 3 Dec 2009 13:46

Re: Referencing a variable?

Yes!  That did it!

First time I've used 'perform:'; I'd never have considered it.

Thank you so much!

Amir

On 3 Dec 2009, at 12:05, Bert Freudenberg wrote:

> On 03.12.2009, at 12:59, Amir Ansari wrote:
>>
>> But I'd like to reuse the code as much as possible, so I'd prefer to 
>> do something like this:
>>
>> element := 'title'.
>> choice := myLibrary collect: [:each | each element].
>
> element := #title.
> choice := myLibrary collect: [:each | each perform: element].
>
> Note that this sends the "title" message. There is no way to directly 
> access an instance variable from outside the object. Objects in 
> Smalltalk are true objects, not data structures. The code above only 
> works if you implemented a "title" method that returns the instance 
> variable.
>
> - Bert -
>
>
(Continue reading)

Bert Freudenberg | 3 Dec 2009 13:49
Picon
Gravatar

Re: Referencing a variable?

On 03.12.2009, at 13:46, Amir Ansari wrote:
> 
> Yes!  That did it!
> 
> First time I've used 'perform:'; I'd never have considered it.
> 
> Thank you so much!
> 
> Amir

You just graduated into the first level of meta-programming :)

- Bert -

Gmane