On Sun, Nov 28, 2010 at 1:37 PM, James Ladd <[email protected]> wrote:
> Hi All,
>
> I'm wondering about documentation to describe the block semantics of Pharo
> Smalltalk,
> Specifically around the return context when a block contains a return: [ ^
> foo ]
>
> Is a block valid only within the method that defined it and passed / called
> it?
> I think it is, and when called outside of the context there is a
> BlockContext no longer exists error.
>
Note that BlockContext is no longer used. | b | b := [thisContext class].
{ b class. b value } answers { BlockClosure. MethodContext }.
The block is valid, but the attempt to return from a method can only be made
once, whether made form an embedded block or the mehtod itself. So the
first attempt to retur wins. However, a block =containing a return can
still be evaluated. An error will occur only if the retrn is actually
executed.
So e.g.
fibBlock
| b |
^b := [:n| n < 0 ifTrue: [^self error: 'Nooooooo!']. n < 2 ifTrue:
[1] ifFalse: [(b value: n - 1) + (b value: n - 2)]]
(0 to: 10) collect: self fibBlock
works fine but
self fibBlock value: -1
will produce a cannot return error.
> However, I'm looking for documentation or confirmation to ensure my testing
> was correct.
>
> Rgs, James.
>