<div dir="ltr"><div dir="ltr"><div dir="ltr">On Mon, Oct 21, 2024 at 10:16 AM Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com" target="_blank">maurizio.cimadamore@oracle.com</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>

  
  <div><p>I get that! What I'm saying is that most users will read my above
      example like:</p>
    <pre style="font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;background-color:rgb(248,248,248);white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block">for (int j = 0 ; j < 10 ; ) {

   ...
   j = j + i;
}
</code></pre>
    <p>That is a good explanation for what the loop does. But this now
      breaks the generalization (because "j" is now mutated inside the
      body). E.g. the variable "j" is declared in the "for loop"
      statement. It is also modified in the for loop statement (in the
      STEP part). It seems mostly an implementation "trick" that we can
      look at STEP before BODY, so that we can then consider the
      induction variable as "not mutated after it is introduced".</p></div></blockquote></div><div><br></div><div>Yes but I think this "trick" is appropriate because it aligns with how developers intuitively view this whole issue.</div><div><br></div><div>In other words, the following two examples may be equivalent in terms of DA/DU analysis, but they are not the same in terms of how ambiguous they seem to the developer:</div><div><br></div><div>Example A:</div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">for (int i = 0; i < 10; i++) {</span></div><div style="margin-left:40px"><span style="font-family:monospace">    Runnable r = () -> System.out.println(i);</span></div><div style="margin-left:40px"><span style="font-family:monospace">}</span></div><div><br></div><div>Example B:</div><div><br></div><div><div style="margin-left:40px"><span style="font-family:monospace">for (int i = 0; i < 10; ) {</span></div><div style="margin-left:40px"><span style="font-family:monospace">    Runnable r = () -> System.out.println(i);</span></div><div style="margin-left:40px"><span style="font-family:monospace">    i++;<br></span></div><div style="margin-left:40px"><span style="font-family:monospace">}</span></div><div><br></div><div>I am claiming that Example A is much clearer than Example B in terms of ambiguity (which is the whole problem "effectively final" was invented to solve).<br></div><div><br></div><div><div>Now the above claim is a claim about what developers 
intuitively perceive, so it's fuzzy by nature and I could certainly be 
in the minority view here!</div><div><br></div><div>All I can 
confidently say is that after thinking about it and looking at the 
examples in the JDK, my own developer intuition would be comfortable 
with an "effectively frozen at the point of capture" rule, and I would 
be pleased with the resulting ability to get rid of almost all of my 
"dummy variables". To me Example A is perfectly clear, while Example B is less clear.<br></div><div><br></div><div>I'm curious what your "developer intuition" thinks. Do you look at Examples A and B and perceive them as equally ambiguous?<br></div><div><br></div></div><div>Taking a step back, the observation that motivates the original proposal (to me at least) is that developers perceive each iteration of the body of a for() loop as its own thing, with the loop variable serving the same role as a method parameter. That is, the body of a for() loop is just a parameterized block of code - i.e., a thing we usually call a "method" - that you "invoke" multiple times, and the loop variable is the parameter. One can view a for() loop as just a special syntax for inlining a method that you want to invoke multiple times in succession with a sequence of parameter values. So just as method parameters are effectively final within their methods, so should loop variables be "effectively final" within their loop bodies.</div><div><br></div><div>If you take that view, then Example A and Example B are clearly different.</div><div><br></div><div>They are just as different as these two examples would be:<br></div><div><br></div><div><div>Example C:</div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">void meth(int i) {</span></div><div style="margin-left:40px"><span style="font-family:monospace">    Runnable r = () -> System.out.println(i);</span></div><div style="margin-left:40px"><span style="font-family:monospace">}</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">for (int i = 0; i < 10; i++)<br></span></div><div style="margin-left:40px"><span style="font-family:monospace">    meth(i);<br></span></div><br><div>Example D:</div><div><br></div><div><div style="margin-left:40px"><span style="font-family:monospace">void meth(i) {</span></div><div style="margin-left:40px"><span style="font-family:monospace">    Runnable r = () -> System.out.println(i);</span></div><div style="margin-left:40px"><span style="font-family:monospace">    i++;<br></span></div><div style="margin-left:40px"><span style="font-family:monospace">}</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">for (int i = 0; i < 10; ) {</span></div><div style="margin-left:40px"><span style="font-family:monospace">    meth(i);</span></div><div style="margin-left:40px"><span style="font-family:monospace">    i++;<br></span></div><div style="margin-left:40px"><span style="font-family:monospace">}</span></div></div></div></div></div><div><br></div><div>Again, we're in the domain of "developer intuition" and "perception" so things are fuzzy... and compiler developers may not constitute a representative sample :)<br></div><div><br></div><div>-Archie</div><div><br></div><div dir="ltr"><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>
</div>