<div dir="ltr"><div dir="ltr">After some reflection on this, I'd like to propose the following.</div><div dir="ltr"><br></div><div>To recap, the problem being addressed is that for() code like this doesn't compile:<br></div><div style="margin-left:40px"><pre class="gmail-notranslate"><span class="gmail-pl-k">for</span> (<span class="gmail-pl-smi">int</span> <span class="gmail-pl-s1">i</span> = <span class="gmail-pl-c1">0</span>; <span class="gmail-pl-s1">i</span> < <span class="gmail-pl-c1">10</span>; <span class="gmail-pl-s1">i</span>++) {
<span class="gmail-pl-smi">Runnable</span> <span class="gmail-pl-s1">r</span> = () -> <span class="gmail-pl-smi">System</span>.<span class="gmail-pl-s1">out</span>.<span class="gmail-pl-en">println</span>(<span class="gmail-pl-s1">i</span>); <span class="gmail-pl-c">// error: "i" is not effectively final</span>
}</pre></div><div>There are two separate ideas proposed in JDK-8300691:</div><div><br></div><div>1. Allow for() loop iteration variables to be declared final but still be mutated in the step</div><div>2. Make for() loop iterations variables be effectively final at the start of the body</div><div><br></div><div>Both ideas would both solve the stated problem.<br></div><div><br></div><div>Idea #1 would include an additional benefit, which is that knowing the variable is final in the body of the loop aids the developer when reasoning about that code.</div><div><br></div><div>However, idea #1 also has some subtle issues (see JDK-8300691 for details) and is somewhat awkward in creating a "final" variable that can still be reassigned.<br></div><div><br></div><div>Idea #2 is straightforward and seems to be intuitively agreeable. It solves the original problem with minimal fuss.<br></div><div><br></div><div>The added benefit of Idea #1 doesn't seem worth the extra mental complexity.<br></div><div><br></div><div>Therefore, I think we should stick with idea #2 for now, i.e., just make for() loop iterations variables be effectively final at the start of the body.</div><div><br></div><div>Thoughts?<br></div><div><br></div><div>-Archie</div><div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div></div>