[Pharo-project] Block explained continued
I did another pass on the Block chapter I'm writing and I would like to know if I'm not saying something wrong. Here is what I wrote. The following examples shows that escaping blocks jumped to their home context but do not unwind the stack after this point. \ct{valueWithExit} is defined on the class BlockClosure as follows. \begin{code}{} BlockClosure>>valueWithExit self value: [ ^nil ] \end{code} \begin{code}{} BExp>>assert: aBoolean aBoolean ifFalse: [Error signal] BExp>>testValueWithExitBreak | val | [ :break | 1 to: 10 do: [ :i | val := i. i = 4 ifTrue: [break value]. ] ] valueWithExit. self assert: val = 4. \end{code} \ct{testValueWithExitBreak} shows that the block \ct{break} is created and activated once and as effect it cancels the rest of the computation. The following method \ct{testValueWithExitContinue} shows that just the computation left from the block activation is skipped (here \ct{val := val + 1. last := i}, when i = 4) and this is why the value of \ct{val} is 9 and not 10. In this example, a new block is created \begin{code}{} BExp>>testValueWithExitContinue | val last | val := 0. 1 to: 10 do: [ :i | [ :continue | i = 4 ifTrue: [continue value]. val := val + 1. last := i ] valueWithExit. ]. self assert: val = 9. self assert: last = 10. \end{code} Pay attention here \ct{valueWithExit} is not equivalent to \ct{value: [^nil]} because it changes the home context of the block. In the first case the homeContext of the block is not the method \ct{testValueWithExitContinue} while in the second it is. Put a self halt before the assert: to convince you. In one case, you will reach the halt while in the other not.
On 1/29/12 1:40 PM, "Stéphane Ducasse" <stephane.ducasse@inria.fr> wrote:
I did another pass on the Block chapter I'm writing and I would like to know if I'm not saying something wrong. Here is what I wrote.
The following examples shows that escaping blocks jumped to their home context but do not unwind the stack after this point. \ct{valueWithExit} is defined on the class BlockClosure as follows.
\begin{code}{} BlockClosure>>valueWithExit self value: [ ^nil ] \end{code}
\begin{code}{} BExp>>assert: aBoolean aBoolean ifFalse: [Error signal]
BExp>>testValueWithExitBreak | val | [ :break | 1 to: 10 do: [ :i | val := i. i = 4 ifTrue: [break value]. ] ] valueWithExit. self assert: val = 4. \end{code}
\ct{testValueWithExitBreak} shows that the block \ct{break} is created and activated once and as effect it cancels the rest of the computation.
The following method \ct{testValueWithExitContinue} shows that just the computation left from the block activation is skipped (here \ct{val := val + 1. last := i}, when i = 4) and this is why the value of \ct{val} is 9 and not 10. In this example, a new block is created
\begin{code}{} BExp>>testValueWithExitContinue | val last | val := 0. 1 to: 10 do: [ :i | [ :continue | i = 4 ifTrue: [continue value]. val := val + 1. last := i ] valueWithExit. ]. self assert: val = 9. self assert: last = 10. \end{code}
Pay attention here \ct{valueWithExit} is not equivalent to \ct{value: [^nil]} because it changes the home context of the block. In the first case the homeContext of the block is not the method \ct{testValueWithExitContinue} while in the second it is. Put a self halt before the assert: to convince you. In one case, you will reach the halt while in the other not.
Happy you was writing a Block chapter. Add as many examples you found and say where we could read some preview. If you wish I volunteer on help with Spanish translate when ready. As always you work inspire us in SqueakRos (181 members today) Edgar
but I'm not finished. There are points I want to really understand. On Jan 29, 2012, at 3:52 PM, Edgar J. De Cleene wrote:
On 1/29/12 1:40 PM, "Stéphane Ducasse" <stephane.ducasse@inria.fr> wrote:
I did another pass on the Block chapter I'm writing and I would like to know if I'm not saying something wrong. Here is what I wrote.
The following examples shows that escaping blocks jumped to their home context but do not unwind the stack after this point. \ct{valueWithExit} is defined on the class BlockClosure as follows.
\begin{code}{} BlockClosure>>valueWithExit self value: [ ^nil ] \end{code}
\begin{code}{} BExp>>assert: aBoolean aBoolean ifFalse: [Error signal]
BExp>>testValueWithExitBreak | val | [ :break | 1 to: 10 do: [ :i | val := i. i = 4 ifTrue: [break value]. ] ] valueWithExit. self assert: val = 4. \end{code}
\ct{testValueWithExitBreak} shows that the block \ct{break} is created and activated once and as effect it cancels the rest of the computation.
The following method \ct{testValueWithExitContinue} shows that just the computation left from the block activation is skipped (here \ct{val := val + 1. last := i}, when i = 4) and this is why the value of \ct{val} is 9 and not 10. In this example, a new block is created
\begin{code}{} BExp>>testValueWithExitContinue | val last | val := 0. 1 to: 10 do: [ :i | [ :continue | i = 4 ifTrue: [continue value]. val := val + 1. last := i ] valueWithExit. ]. self assert: val = 9. self assert: last = 10. \end{code}
Pay attention here \ct{valueWithExit} is not equivalent to \ct{value: [^nil]} because it changes the home context of the block. In the first case the homeContext of the block is not the method \ct{testValueWithExitContinue} while in the second it is. Put a self halt before the assert: to convince you. In one case, you will reach the halt while in the other not.
Happy you was writing a Block chapter. Add as many examples you found and say where we could read some preview. If you wish I volunteer on help with Spanish translate when ready.
As always you work inspire us in SqueakRos (181 members today)
Edgar
I read your text and am confused about "returing" _from_ or _to_ the homecontext and "escaping to" the homecontext. Do I understand it right this way: - As a block is executed always in it's home-context, when there is an explicit return statement inside the block, this return statement will also be executed in the home-context, which means this return statement makes the execution return *from* the home-context (*not* return from the context in which the value message was sent *to* the home-context?)? - The word 'continuation' is used when there is an explicit return statement in a block - It means the execution continues in the context "before" the home-context? -- View this message in context: http://forum.world.st/Block-explained-continued-tp4338472p4339548.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
I rewrote it. Read this version it is much better. Stef On Jan 30, 2012, at 12:15 AM, Helene Bilbo wrote:
I read your text and am confused about "returing" _from_ or _to_ the homecontext and "escaping to" the homecontext. Do I understand it right this way:
- As a block is executed always in it's home-context, when there is an explicit return statement inside the block, this return statement will also be executed in the home-context, which means this return statement makes the execution return *from* the home-context (*not* return from the context in which the value message was sent *to* the home-context?)?
- The word 'continuation' is used when there is an explicit return statement in a block - It means the execution continues in the context "before" the home-context?
-- View this message in context: http://forum.world.st/Block-explained-continued-tp4338472p4339548.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
participants (3)
-
Edgar J. De Cleene -
Helene Bilbo -
Stéphane Ducasse