Understanding "self"

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

Understanding "self"

Andy Burnett
I am working through the SBE book ( which is really very good) and I  
have been experimenting with using self in blocks.

In simple terms I am doing something like:
" within the initialisation of an object"

aVar := AnotherObj new
aVar setBlockTo: [Transcript show: self]

Then I had a method in the first object which asked the second object  
to show the value of its block.

What I was expecting was for self to be defined in terms of the object  
it was assigned to ie the second object. However it actually returns  
tv details of the object the block was coded in

Somehow I don't think I am explaining myself very well. But what I  
want to know is how to think about self. Will it always point to the  
object in which the method was actually written?

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

Re: Understanding "self"

Mathieu SUEN
Hi Andy,

The scope of the block is taken from were it is define not were it is  
evaluate.
If no then yo couldn't write code like this for example:

aCollection detect:[:each | self maxValue < each]

One solution to what you want could be to add a parameter:


aVar := AnotherObj new
aVar setBlockTo: [:arg | Transcript show: arg]
aVar block value: self.




On Jan 30, 2008, at 6:43 PM, Andy Burnett wrote:

> I am working through the SBE book ( which is really very good) and I  
> have been experimenting with using self in blocks.
>
> In simple terms I am doing something like:
> " within the initialisation of an object"
>
> aVar := AnotherObj new
> aVar setBlockTo: [Transcript show: self]
>
> Then I had a method in the first object which asked the second  
> object to show the value of its block.
>
> What I was expecting was for self to be defined in terms of the  
> object it was assigned to ie the second object. However it actually  
> returns tv details of the object the block was coded in
>
> Somehow I don't think I am explaining myself very well. But what I  
> want to know is how to think about self. Will it always point to the  
> object in which the method was actually written?
>
> Thanks
> AB
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

        Mth



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