[Pharo-project] Beginner question about "self" in block

29 views
Skip to first unread message

Garret Raziel

unread,
Jan 25, 2012, 3:54:22 PM1/25/12
to pharo-...@lists.gforge.inria.fr
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

Benjamin

unread,
Jan 25, 2012, 4:13:34 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
I think the self is bound at the block creation



To be confirm,

Ben

Stéphane Ducasse

unread,
Jan 25, 2012, 4:24:24 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
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 :)

Block.pdf

Garret Raziel

unread,
Jan 25, 2012, 4:29:04 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
Ok, thanks for explanation to all of you. I'll read drafts from PBEII after I finish PBE :-).

Stéphane Ducasse

unread,
Jan 25, 2012, 4:33:26 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
browse it this is short and skip what you find boring or too complex :)

Garret Raziel

unread,
Jan 25, 2012, 4:48:43 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
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

Frank Shearar

unread,
Jan 25, 2012, 4:53:23 PM1/25/12
to Pharo-...@lists.gforge.inria.fr

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

Garret Raziel

unread,
Jan 25, 2012, 5:04:22 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
Ok, so it automaticaly uses longest possible message?

Herby Vojčík

unread,
Jan 25, 2012, 5:30:49 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
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

Garret Raziel

unread,
Jan 25, 2012, 5:55:25 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
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".

Gabriel Cotelli

unread,
Jan 25, 2012, 6:18:31 PM1/25/12
to Pharo-...@lists.gforge.inria.fr
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).

Ben Coman

unread,
Jan 26, 2012, 2:15:32 AM1/26/12
to Pharo-...@lists.gforge.inria.fr

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

unread,
Jan 26, 2012, 2:49:57 AM1/26/12
to Pharo-...@lists.gforge.inria.fr
#>> already does something.  Try printing "String >> #asDate". It fetches a compiled method.
--
Milan Mimica
http://sparklet.sf.net

Ben Coman

unread,
Jan 26, 2012, 3:57:09 AM1/26/12
to Pharo-...@lists.gforge.inria.fr
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.

Stéphane Ducasse

unread,
Jan 26, 2012, 3:59:19 AM1/26/12
to Pharo-...@lists.gforge.inria.fr
this explains in PBE2

Stef

Damien Cassou

unread,
Jan 26, 2012, 4:23:55 AM1/26/12
to Pharo-...@lists.gforge.inria.fr
On Thu, Jan 26, 2012 at 9:57 AM, Ben Coman <b...@openinworld.com> 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

Ben Coman

unread,
Jan 26, 2012, 4:35:07 AM1/26/12
to Pharo-...@lists.gforge.inria.fr

Damien Cassou wrote:
> On Thu, Jan 26, 2012 at 9:57 AM, Ben Coman <b...@openinworld.com> 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.


Tudor Girba

unread,
Jan 26, 2012, 4:47:43 AM1/26/12
to Pharo-...@lists.gforge.inria.fr
I think Damien meant the other Ben(jamin) :)

Cheers,
Doru

--
www.tudorgirba.com

"Every thing has its own flow"

Stéphane Ducasse

unread,
Jan 26, 2012, 4:56:31 AM1/26/12
to Pharo-...@lists.gforge.inria.fr

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 :)

Benjamin

unread,
Jan 26, 2012, 10:09:55 AM1/26/12
to Pharo-...@lists.gforge.inria.fr
I was wondering if he was talking about me or not :)


(the other) Ben ;)

Reply all
Reply to author
Forward
0 new messages