Randal said...<br>
<br>
<<With classic smalltalk 80 and Squeak:<br>
<br>a := (1 to: 10) do: [:n | [n]].<br>
b := a collect: [:each | each value].<br>
b will have something like #(10 10 10 10 ...).<br>
<br>The problem is that the :n in each block ([n]) is shared as one variable.<br>
In a true closure, which modern Squeak and Pharo provide, we get the proper<br>
#(1 2 3 4 ... 10). That's because each apperance of "n" is "closed" with<br>
respect to the newly created block.>><br><br>Thanks for explaining the difference between blocks and closures. I think I new understand that. And, your example raised a new question for me.<br><br>What does the inner 'block' in [:n | [n]] actually do? <br>
<br>Out of curiosity I tried changing it to [:n | [n*10]], expecting the numbers to be ten times bigger, but they were stil 1, 2, 3... I also tried removing the whole block, and b still evaluated to the same values.<br><br>
Cheers<br>Andy<br>