[Newbies] A question about returning values from a block
jgon.in.canada at gmail.com
Wed Dec 30 22:05:17 UTC 2009
If I write the following code to experiment with squeak's block closures I
get the expected answer:
factorial := [:n | (n=1) ifTrue:  ifFalse: [n * (factorial value: (n -
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 -
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,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners