[Newbies] A question about returning values from a block

Jeff Gonis jgon.in.canada at gmail.com
Wed Dec 30 22:05:17 UTC 2009


Hi all,

If I write the following code to experiment with squeak's block closures I
get the expected answer:
factorial := [:n | (n=1) ifTrue: [1] ifFalse: [n * (factorial value: (n -
1))]].
factorial 3. -> 6

In this case it appears that the block is implicitly returning the last
object, ie: return 1 in the ifTrue: case.  If I change the code to
explicitly return the 1 ie:
factorial := [:n | (n=1) ifTrue: [^1] ifFalse: [n * (factorial value: (n -
1))]].
factorial 3. -> An error indicating that the block cannot return.

My question is this:  Why is the implicit return of values allowed and not
the explicit?  Is there a best practice for dealing with block closures in
smalltalk, such that we should only be using implicit returns?

Thanks for your help,
Jeff G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20091230/6b197d76/attachment.htm


More information about the Beginners mailing list