<div class="gmail_quote">On Thu, Jul 5, 2012 at 4:26 PM, Denis Shelomovskij <span dir="ltr"><<a href="mailto:verylonglogin.reg@gmail.com" target="_blank">verylonglogin.reg@gmail.com</a>></span> wrote:<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Different situation is for such C# loop:<br>
---<br>
for (int i = 0; i < funcs.Length; ++i)<br>
{<br>
    int t = i;<br>
    funcs[i] = new MyFunc(() => System.Console.WriteLine(t));<br>
}<br>
---<br>
where "t" is local for scope. Here C# behaves correctly, but D doesn't. This D loop<br>
---<br>
foreach(i; 0 .. 5) {<br>
    int t = i;<br>
    functions ~= { printf("%d\n", t); };<br>
}<br>
---<br>
prints "4" five times. It's Issue 2043:<br>
<a href="http://d.puremagic.com/issues/show_bug.cgi?id=2043" target="_blank">http://d.puremagic.com/issues/<u></u>show_bug.cgi?id=2043<br></a><br></blockquote><div> </div></div><span style="font-family:Arial;font-size:12px;white-space:pre-wrap">How to distinguish which variables will be copied to the closure context?</span><div>
<span style="font-family:Arial;font-size:12px;white-space:pre-wrap"><br></span></div><div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><font lang="en">I think this is a scope rule, in the previous code, there are three variables:</font></div>
<div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><font lang="en">1. function arguments</font></div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><font lang="en">2. loop variables</font></div>
<div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><font lang="en">3. local variables</font></div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><font lang="en">Seems only function parameters is copied. In C#, local variables is copied. </font>There are other rules? And why is the loop variable not local?</div>
</div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><br></div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap">Thanks.</div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap">
<br></div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap">Best regards,</div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap"><br></div><div class="force_dir" style="font-family:Arial;font-size:12px;white-space:pre-wrap">
-- Li Jie</div>