On Fri, Jul 6, 2012 at 3:06 PM, Timon Gehr <span dir="ltr"><<a href="mailto:timon.gehr@gmx.ch" target="_blank">timon.gehr@gmx.ch</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">
<br></div>
It is simple. Variable declarations introduce a new variable. Closures<br>
that reference the same variable will see the same values.<br>
<br>
----<br>
<br>
foreach(i; 0..3) { functions~={writeln(i);}; }<br>
<br>
is the same as<br>
<br>
for(int i=0;i<3;i++) { functions~={writeln(i);}; }<br>
<br>
is the same as<br>
<br>
{int i=0;for(;i<3;i++) { functions~={writeln(i);}; }}<br>
<br>
is the same as<br>
<br>
{<br>
int i=0;<br>
{ functions~={writeln(i);}; }<br>
i++;<br>
{ functions~={writeln(i);}; }<br>
i++;<br>
{ functions~={writeln(i);}; }<br>
i++;<br>
}<br>
<br>
<br>
----<br>
<br>
foreach(i; 0..3){ int j=i; functions~={writeln(j);}; }<br>
<br>
is the same as<br>
<br>
for(int i=0;i<3;i++){ int j=i; functions~={writeln(j);}; }<br>
<br>
is the same as<br>
<br>
{int i=0;for(i<3;i++){ int j=i; functions~={writeln(j);}; }<br>
<br>
is the same as<br>
<br>
{<br>
int i=0;<br>
{ int j=i; functions~={writeln(j);}; }<br>
i++;<br>
{ int j=i; functions~={writeln(j);}; }<br>
i++;<br>
{ int j=i; functions~={writeln(j);}; }<br>
i++;<br>
}<br>
<br>
----<br>
<br>
I think it is quite intuitive.<br>
<br>
</blockquote></div><br><div>Understood, thanks.</div>