On 08.03.2009, at 16:10, Rob Rothwell wrote:
> This is really a newbie question; but I'd thought I'd ask everyone
> who is already talking about it anyway...
>
> Can you explain exactly what IS native closures-support and why it
> will be appreciated by the Squeakers?
>
> What will this allow you to do that was not previously possible?
Simply put, it makes working with blocks more general. Blocks are one
of the "coolest" features of Smalltalk, but the way they work
currently is limited. Consider this:
multiply := Array new: 4.
multiply at: 1 put: [:x | x * 1].
multiply at: 2 put: [:x | x * 2].
multiply at: 3 put: [:x | x * 3].
multiply at: 4 put: [:x | x * 4].
This creates an Array where each element is a Block that multiples an
argument (x) by some constant. E.g.,
(multiply at: 3) value: 5.
would answer 15. So far, so good. Now you might want to put the
creation of these blocks in a loop:
multiply := Array new: 4.
1 to: 4 do: [:i |
multiply at: i put: [:x | x * i].
].
And you would rightfully assume that this is equivalent to the version
above, just more concise. But now try again:
(multiply at: 3) value: 5.
The answer will, surprisingly, not be 15 in current Squeak. But with
closures, the blocks would behave as expected. They are said to "close
over" the state that the bound variables (i in this case) had at the
time the block was created.
So basically, you can use blocks as you always have, but they will
behave as you might have assumed they would.
- Bert -
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners