[Newbies] Re: [squeak-dev] Re: New Window VM (Closures, FT2Plugin, Large Cursors)

Bert Freudenberg bert at freudenbergs.de
Sun Mar 8 16:38:47 UTC 2009

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 -

More information about the Beginners mailing list