Beginner question about "self" in block

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

Beginner question about "self" in block

Garret Raziel
Hi, I have one begginer question. It is may be simple, but it very baffles me.

I am reading Pharo by Example (great book btw, thanks!). I'm in chapter two where I'm creating Lights Out game. There is this simple code http://pastebin.com/eQregZ35. What baffles me is line 10. I assign "Block of code" to mouseAction variable of LOCell. In this Block, there is "self", that obviously refers to LOGame object in that time. But when is this Block actualy EVALUATED (when I click on Cell), "self" should be reffering to LOCell object, isn't it? If I inspect one LOCell, inspector shows that it has instance variable "mouseAction", which is BlockClosure, that looks like "[self toggleNeighboursOfCellAt: i at: j]". If "LOCell" is the "owner" of this block and also it is the one that evaluates it, shouldn't "self" refer to LOCell object? How BlockClosure desides where "self" actually points?

Thanks for explanation.
Jan
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Benjamin Van Ryseghem (Pharo)
I think the self is bound at the block creation



To be confirm,

Ben
On Jan 25, 2012, at 9:54 PM, Garret Raziel wrote:

Hi, I have one begginer question. It is may be simple, but it very baffles me.

I am reading Pharo by Example (great book btw, thanks!). I'm in chapter two where I'm creating Lights Out game. There is this simple code http://pastebin.com/eQregZ35. What baffles me is line 10. I assign "Block of code" to mouseAction variable of LOCell. In this Block, there is "self", that obviously refers to LOGame object in that time. But when is this Block actualy EVALUATED (when I click on Cell), "self" should be reffering to LOCell object, isn't it? If I inspect one LOCell, inspector shows that it has instance variable "mouseAction", which is BlockClosure, that looks like "[self toggleNeighboursOfCellAt: i at: j]". If "LOCell" is the "owner" of this block and also it is the one that evaluates it, shouldn't "self" refer to LOCell object? How BlockClosure desides where "self" actually points?

Thanks for explanation.
Jan

Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Stéphane Ducasse
In reply to this post by Garret Raziel
Welcome


> Hi, I have one begginer question. It is may be simple, but it very baffles me.
>
> I am reading Pharo by Example (great book btw, thanks!). I'm in chapter two where I'm creating Lights Out game. There is this simple code http://pastebin.com/eQregZ35. What baffles me is line 10. I assign "Block of code" to mouseAction variable of LOCell. In this Block, there is "self", that obviously refers to LOGame object in that time. But when is this Block actualy EVALUATED (when I click on Cell), "self" should be reffering to LOCell object, isn't it? If I inspect one LOCell, inspector shows that it has instance variable "mouseAction", which is BlockClosure, that looks like "[self toggleNeighboursOfCellAt: i at: j]". If "LOCell" is the "owner" of this block and also it is the one that evaluates it, shouldn't "self" refer to LOCell object? How BlockClosure desides where "self" actually points?
>
> Thanks for explanation.

Here is a draft of a next chapter on block :)
But I should finish it :)

But I want to add how block are implemented at the bye code level so it take times because not that many people are helping, so I have to learn first.

Stef



Block.pdf (322K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Garret Raziel
Ok, thanks for explanation to all of you. I'll read drafts from PBEII after I finish PBE :-).
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Stéphane Ducasse
browse it this is short and skip what you find boring or too complex :)

On Jan 25, 2012, at 10:29 PM, Garret Raziel wrote:

> Ok, thanks for explanation to all of you. I'll read drafts from PBEII after I finish PBE :-).


Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Garret Raziel
I have another question, too short to create another thread. What happens if some object has method a: and b: AND a:b:? For example, SmallInt has method raisedTo:modulo:, but what will happen, if I define methods SmallInt>>raisedTo: and SmallIt>>modulo: and then execute "5 raisedTo: 6 modulo: 3"? Will it use "raisedTo:modulo:" method or will it use "raisedTo:" method and then send "modulo:" method to the result?

Jan
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Frank Shearar-3
On 25 January 2012 21:48, Garret Raziel <[hidden email]> wrote:
> I have another question, too short to create another thread. What happens if
> some object has method a: and b: AND a:b:? For example, SmallInt has method
> raisedTo:modulo:, but what will happen, if I define methods
> SmallInt>>raisedTo: and SmallIt>>modulo: and then execute "5 raisedTo: 6
> modulo: 3"? Will it use "raisedTo:modulo:" method or will it use "raisedTo:"
> method and then send "modulo:" method to the result?
>
> Jan

The message names are greedy, so in your example "5 raisedTo: 6
modulo: 3" will send the #raisedTo:modulo: message to 5 with arguments
6 and 3. If you wanted to send #raisedTo: and then #modulo: you'd have
to use parentheses: "(5 raisedTo: 6) modulo: 3".

frank

Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Garret Raziel
Ok, so it automaticaly uses longest possible message?
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Herby Vojčík
Garret Raziel wrote:
> Ok, so it automaticaly uses longest possible message?

Not the longest (in: the longest available), but whole.
Concatenation of all xxx: keyword in the whole statement or
parenthesised expression is used. I doesn't matter if #foo: exists if
statement is 1 foo: 3 bar: 5; it is always #foo:bar:.

Herby

Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Garret Raziel
So, keyword messages are slightly different from binary messages, because 5+4*3 doesn't invoke "+*" message so I don't have to write "(5+4)*3". If I had SmallInteger>>#multiplyBy and SmallInteger>>#add, I should use parenthesis "(5 add: 3) multiplyBy: 3".
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

gcotelli
Yes, Binary messages always involves two objects, so in your example there's two message sends. Be aware also that there's no arithmetic specific precedence rules  ( 4  * 5 + 2  will return 22). The precedence is consistent among the whole language (unary->binary->keyworded).

On Wed, Jan 25, 2012 at 7:55 PM, Garret Raziel <[hidden email]> wrote:
So, keyword messages are slightly different from binary messages, because 5+4*3 doesn't invoke "+*" message so I don't have to write "(5+4)*3". If I had SmallInteger>>#multiplyBy and SmallInteger>>#add, I should use parenthesis "(5 add: 3) multiplyBy: 3".

Reply | Threaded
Open this post in threaded view
|

Noobs perspective on pasting example code

Ben Coman
In reply to this post by Stéphane Ducasse

Just a passing thought....

It is common convention for example methods to be presented in text
using '>>' preceded by the class.  This is obviously needed to define
the class/method relationship outside the image, but the whole example
cannot be pasted directly into System Browser as the '>>' is not part of
the Smalltalk syntax for defining methods.  

For those new to Pharo going through tutorials, copy/pasting the whole
of the presented code eg [1] returns only the error  "Nothing more
expected" which is a bit cryptic to noobs who expect to follow the
example verbatim.  Once past understanding this, it continues (for me)
to be a minor annoyance to have to select only the text following the '>>'.

I wonder whether it would be beneficial for the compiler to handle  
'>>'  at the start of a method definition.  The System Browser would
then jump to the created method.  As well as beneficial to those
experiencing Pharo for the first time, this might be useful as a general
shortcut such that when browsing one class you can define a method for
another class without first having to browse to that class.

cheers, Ben

[1]
BExp>>testBlock: aBlock
| t |
t := nil.
aBlock value



Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

mmimica
#>> already does something.  Try printing "String >> #asDate". It fetches a compiled method.

On 26 January 2012 08:15, Ben Coman <[hidden email]> wrote:

Just a passing thought....
It is common convention for example methods to be presented in text using '>>' preceded by the class.  This is obviously needed to define the class/method relationship outside the image, but the whole example cannot be pasted directly into System Browser as the '>>' is not part of the Smalltalk syntax for defining methods.  
For those new to Pharo going through tutorials, copy/pasting the whole of the presented code eg [1] returns only the error  "Nothing more expected" which is a bit cryptic to noobs who expect to follow the example verbatim.  Once past understanding this, it continues (for me) to be a minor annoyance to have to select only the text following the '>>'.

I wonder whether it would be beneficial for the compiler to handle  '>>'  at the start of a method definition.  The System Browser would then jump to the created method.  As well as beneficial to those experiencing Pharo for the first time, this might be useful as a general shortcut such that when browsing one class you can define a method for another class without first having to browse to that class.

cheers, Ben

[1]
BExp>>testBlock: aBlock
| t |
t := nil.
aBlock value






--
Milan Mimica
http://sparklet.sf.net
Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

Ben Coman
Thanks Milan. Something new I've learnt.  However I don't think this is
a conflict.
While your example works from Workspace, go where you define a method
and try to get the following accepted.

String >> #asDate
    ^1

The above is not valid, so no conflict in this context.


Milan Mimica wrote:

> #>> already does something.  Try printing "String >> #asDate". It fetches a
> compiled method.
>
> On 26 January 2012 08:15, Ben Coman <[hidden email]> wrote:
>
>  
>> Just a passing thought....
>> It is common convention for example methods to be presented in text using
>> '>>' preceded by the class.  This is obviously needed to define the
>> class/method relationship outside the image, but the whole example cannot
>> be pasted directly into System Browser as the '>>' is not part of the
>> Smalltalk syntax for defining methods.
>> For those new to Pharo going through tutorials, copy/pasting the whole of
>> the presented code eg [1] returns only the error  "Nothing more expected"
>> which is a bit cryptic to noobs who expect to follow the example verbatim.
>>  Once past understanding this, it continues (for me) to be a minor
>> annoyance to have to select only the text following the '>>'.
>>
>> I wonder whether it would be beneficial for the compiler to handle  '>>'
>>  at the start of a method definition.  The System Browser would then jump
>> to the created method.  As well as beneficial to those experiencing Pharo
>> for the first time, this might be useful as a general shortcut such that
>> when browsing one class you can define a method for another class without
>> first having to browse to that class.
>>
>> cheers, Ben
>>
>> [1]
>> BExp>>testBlock: aBlock
>> | t |
>> t := nil.
>> aBlock value
>>
>>
>>
>>
>>    
>
>
>  


Reply | Threaded
Open this post in threaded view
|

Re: Beginner question about "self" in block

Stéphane Ducasse
In reply to this post by Garret Raziel
this explains in PBE2

Stef

On Jan 25, 2012, at 11:04 PM, Garret Raziel wrote:

> Ok, so it automaticaly uses longest possible message?


Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

Damien Cassou
In reply to this post by Ben Coman
On Thu, Jan 26, 2012 at 9:57 AM, Ben Coman <[hidden email]> wrote:
> Thanks Milan. Something new I've learnt.  However I don't think this is a
> conflict.

I agree, I don't see any conflict. I also like the proposal. Ben, do
you think you could try to implement a prototype? I advise you to
implement it either for OmniBrowser or for Nautilus and see if it
makes sense.

Thanks for the proposal

--
Damien Cassou
http://damiencassou.seasidehosting.st

"Lambdas are relegated to relative obscurity until Java makes them
popular by not having them." James Iry

Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

Ben Coman

Damien Cassou wrote:

> On Thu, Jan 26, 2012 at 9:57 AM, Ben Coman <[hidden email]> wrote:
>  
>> Thanks Milan. Something new I've learnt.  However I don't think this is a
>> conflict.
>>    
>
> I agree, I don't see any conflict. I also like the proposal. Ben, do
> you think you could try to implement a prototype? I advise you to
> implement it either for OmniBrowser or for Nautilus and see if it
> makes sense.
>
> Thanks for the proposal
>
>  
I can't do anything for the next six months - I'm a long way behind
where I wanted to be with my dissertation.
I'll be happy to give it a go after that is complete.  For now, I just
let out the idea while it occurs to me, so it does get lost.


Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

Tudor Girba-2
I think Damien meant the other Ben(jamin) :)

Cheers,
Doru


On Thu, Jan 26, 2012 at 10:35 AM, Ben Coman <[hidden email]> wrote:

>
> Damien Cassou wrote:
>>
>> On Thu, Jan 26, 2012 at 9:57 AM, Ben Coman <[hidden email]> wrote:
>>
>>>
>>> Thanks Milan. Something new I've learnt.  However I don't think this is a
>>> conflict.
>>>
>>
>>
>> I agree, I don't see any conflict. I also like the proposal. Ben, do
>> you think you could try to implement a prototype? I advise you to
>> implement it either for OmniBrowser or for Nautilus and see if it
>> makes sense.
>>
>> Thanks for the proposal
>>
>>
>
> I can't do anything for the next six months - I'm a long way behind where I
> wanted to be with my dissertation.
> I'll be happy to give it a go after that is complete.  For now, I just let
> out the idea while it occurs to me, so it does get lost.
>
>



--
www.tudorgirba.com

"Every thing has its own flow"

Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

Stéphane Ducasse
In reply to this post by Ben Coman

On Jan 26, 2012, at 8:15 AM, Ben Coman wrote:

>
> Just a passing thought....
> It is common convention for example methods to be presented in text using '>>' preceded by the class.  This is obviously needed to define the class/method relationship outside the image, but the whole example cannot be pasted directly into System Browser as the '>>' is not part of the Smalltalk syntax for defining methods.  
> For those new to Pharo going through tutorials, copy/pasting the whole of the presented code eg [1] returns only the error  "Nothing more expected" which is a bit cryptic to noobs who expect to follow the example verbatim.  Once past understanding this, it continues (for me) to be a minor annoyance to have to select only the text following the '>>'.

would be nice!

> I wonder whether it would be beneficial for the compiler to handle  '>>'  at the start of a method definition.  The System Browser would then jump to the created method.  As well as beneficial to those experiencing Pharo for the first time, this might be useful as a general shortcut such that when browsing one class you can define a method for another class without first having to browse to that class.

Ideally I would like to have also the support for coral syntax :)

>
> cheers, Ben
>
> [1]
> BExp>>testBlock: aBlock
> | t |
> t := nil.
> aBlock value
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Noobs perspective on pasting example code

Benjamin Van Ryseghem (Pharo)
In reply to this post by Tudor Girba-2
I was wondering if he was talking about me or not :)


(the other) Ben ;)

On Jan 26, 2012, at 10:47 AM, Tudor Girba wrote:

> I think Damien meant the other Ben(jamin) :)
>
> Cheers,
> Doru
>
>
> On Thu, Jan 26, 2012 at 10:35 AM, Ben Coman <[hidden email]> wrote:
>>
>> Damien Cassou wrote:
>>>
>>> On Thu, Jan 26, 2012 at 9:57 AM, Ben Coman <[hidden email]> wrote:
>>>
>>>>
>>>> Thanks Milan. Something new I've learnt.  However I don't think this is a
>>>> conflict.
>>>>
>>>
>>>
>>> I agree, I don't see any conflict. I also like the proposal. Ben, do
>>> you think you could try to implement a prototype? I advise you to
>>> implement it either for OmniBrowser or for Nautilus and see if it
>>> makes sense.
>>>
>>> Thanks for the proposal
>>>
>>>
>>
>> I can't do anything for the next six months - I'm a long way behind where I
>> wanted to be with my dissertation.
>> I'll be happy to give it a go after that is complete.  For now, I just let
>> out the idea while it occurs to me, so it does get lost.
>>
>>
>
>
>
> --
> www.tudorgirba.com
>
> "Every thing has its own flow"
>


12