Referencing a variable?

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

Referencing a variable?

Amir Ansari
Dear list

I've been looking through books, blogs and lists for a solution to this
problem, but can't find anything!  Which means that I'm not thinking
about it in the right way...  I'd be very grateful if someone could
enlighten me!


I have myLibrary (an OrderedCollection) of Books (class with 'title'
and 'author' instance variables):

myLibrary := OrderedCollection new.
myLibrary
        add: (Book new
                title: 'Hamlet';
                author: 'Shakespeare');
        add: (Book new
                title: 'The Hobbit';
                author: 'Tolkien');
        add: (Book new
                title: 'Ben Hur';
                author: 'Wallace').


Now, if I want to gather all the titles and authors into new
collections, I can do the following:

titles := myLibrary collect: [:each | each title].
authors := myLibrary collect: [:each | each author].


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].

So I'm trying to refer to the instance variables 'title' or 'author'
using another variable called 'element'.  But it doesn't work, and I
can't figure out another way to do it...

Is there another solution?

Thanks!

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Referencing a variable?

Bert Freudenberg
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 -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Referencing a variable?

Amir Ansari
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 -
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Referencing a variable?

Bert Freudenberg
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 -

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners