<div dir="ltr"><div dir="ltr">On Tue, Jan 7, 2025 at 9:54 AM Attila Kelemen <<a href="mailto:attila.kelemen85@gmail.com">attila.kelemen85@gmail.com</a>> wrote:</div><div class="gmail_quote gmail_quote_container"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I didn't mean for the lambda to unassign the variable, I was just lazy to write it out exactly: I meant that the variable is DU after lambda if we assume that the variable is DU at the point of capturing (basically we pretend that the variable is DU even though it is actually DA). Consider this code (doesn't compile, but I think it should).</div><div><br></div><div>int x = 5;</div><div>if (test) x = 6;</div><div>new Thread(() -> { System.out.println("Hello: " + x) }).start();</div><div><br></div><div>I don't really see why this shouldn't compile.</div></div></blockquote><div><br></div><div>In the for() loop discussion, we used the phrase "not reassigned" for this concept.</div><div><br></div><div>That thread ("JDK-8300691 - final variables in for loop headers should accept updates") has a bunch of discussion about the idea.<br></div><div><br></div><div>One wrinkle is that properly defining "not reassigned" requires properly defining "before" and "after", and for() loops complicate that definition.</div><div><br></div><div>For example if you have this:</div><div style="margin-left:40px"><pre>for (int i = 1; i <= 3; i++) {
Runnable r = () -> System.out.println(i); // allow this?
}
</pre></div><div>The "i++" happens "after" the capture - assuming your definition of "after" is based on control flow rather than lexical position - which means we'd need a special exception for it, etc.</div><div><br></div><div>John Rose had some relevant thoughts/discussion here (re: "final shadowing"): <a href="https://mail.openjdk.org/pipermail/amber-dev/2024-October/008996.html">https://mail.openjdk.org/pipermail/amber-dev/2024-October/008996.html</a></div><div><br></div></div><div class="gmail_quote gmail_quote_container">-Archie<br></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>