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 |
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. |
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 |
Ok, thanks for explanation to all of you. I'll read drafts from PBEII after I finish PBE :-).
|
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 :-). |
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 |
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 |
Ok, so it automaticaly uses longest possible message?
|
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 |
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".
|
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". |
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 |
#>> already does something. Try printing "String >> #asDate". It fetches a compiled method.
On 26 January 2012 08:15, Ben Coman <[hidden email]> wrote:
Milan Mimica http://sparklet.sf.net |
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 >> >> >> >> >> > > > |
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? |
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 |
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 > > 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. |
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" |
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 > > > |
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" > |
Free forum by Nabble | Edit this page |