<div class="gmail_quote">Ok...thanks...this is making sense!<br><br>So...should I get the &quot;right answer&quot; with the new VM and the 3.10.2-Closures image?<br><br>Because...I still get 25!<br><br>Rob<br><br>On Sun, Mar 8, 2009 at 2:01 PM, Bert Freudenberg <span dir="ltr">&lt;<a href="mailto:bert@freudenbergs.de">bert@freudenbergs.de</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On 08.03.2009, at 18:13, Rob Rothwell wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sun, Mar 8, 2009 at 12:38 PM, Bert Freudenberg &lt;<a href="mailto:bert@freudenbergs.de" target="_blank">bert@freudenbergs.de</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
multiply := Array new: 4.<br>
1 to: 4 do: [:i |<br>
       multiply at: i put: [:x | x * i].<br>
].<br>
<br>
And you would rightfully assume that this is equivalent to the version above, just more concise. But now try again:<br>
<br>
(multiply at: 3) value: 5.<br>
<br>
The answer will, surprisingly, not be 15 in current Squeak.<br>
</blockquote>
<br>
You are right!  25, in fact...<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
But with closures, the blocks would behave as expected. They are said to &quot;close over&quot; the state that the bound variables (i in this case) had at the time the block was created.<br>
</blockquote>
<br>
So...VW does this &quot;right,&quot; I guess, since I get 15 in VW NC 7.6?<br>
</blockquote>
<br></div>
Yes, it has block closures.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

So basically, you can use blocks as you always have, but they will behave as you might have assumed they would.<br>
<br>
</blockquote>
So the recent VM work discussed handles this?  I will have to give that a try!<br>
<br>
Thanks for the explanation!  The next question, of course, is WHY does it get 25 and not 15... ;)<br>
</blockquote>
<br>
<br></div>
Because instead of &quot;closing over&quot; the current value of i when the block is created, all the blocks share the same reference to i, which is actually another temporary variable of the method, instead of being local to the block. So when the block is evaluated later, the last value of i is used.<br>

<br>
Now you might still expect that last value of i to be 4, but actually &quot;1 to: 4 do:&quot; is expanded by the compiler to a while loop like &quot;i := 1. [i &lt;= 4] whileTrue: [i := i + 1]&quot;, so it actually is 5 after the loop terminates.<br>

<br>
And all this is avoided by having real closures, the i would indeed be local to the block and not a shared temp.<br><font color="#888888">
<br>
- Bert -<br>
<br>
<br>
</font></blockquote></div><br>