[Newbies] Closure support

Randal L. Schwartz merlyn at stonehenge.com
Thu Oct 29 00:07:31 UTC 2009

>>>>> "Alex" == Alex Schenkman <alex at schenkman.info> writes:

Alex> What does this mean?
Alex> A closure is a block, right? And Squeak has already blocks...

A closure is a block but not all blocks are closures.

With classic smalltalk 80 and Squeak:

a := (1 to: 10) do: [:n | [n]].
b := a collect: [:each | each value].

b will have something like #(10 10 10 10 ...).

The problem is that the :n in each block ([n]) is shared as one variable.

In a true closure, which modern Squeak and Pharo provide, we get the proper
#(1 2 3 4 ... 10).  That's because each apperance of "n" is "closed" with
respect to the newly created block.

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

More information about the Beginners mailing list