<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:monospace">I forgot to click Reply All, so most of this conversation happened off of the mailing list. Read below to see the discussion, as well as an example of what Tagir was talking about, as well as potential workarounds.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 15, 2024 at 4:48 AM Tagir Valeev <<a href="mailto:amaembo@gmail.com">amaembo@gmail.com</a>> wrote:<br></div><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 dir="ltr">Probably. By the way, the original piece of code I usually write as:<div><br></div><div><div style="font-family:monospace">while(true) {</div><div style="font-family:monospace"> final var someVar = someMethod();<br></div><div style="font-family:monospace"> doSomethingWith(someVar);<br></div><div style="font-family:monospace"> //etc.<br></div><div style="font-family:monospace"> if (!someVar.someBooleanReturningMethod()) break;</div><div style="font-family:monospace">}</div></div></div><br><div class="gmail_quote"><div class="gmail_attr">Not so pretty too, but still allows keeping the scope of someVar limited and avoiding splitting the declaration and the assignment.</div><div class="gmail_attr"><br></div><div class="gmail_attr">With best regards,</div><div class="gmail_attr">Tagir Valeev.</div><div dir="ltr" class="gmail_attr"><br></div><div dir="ltr" class="gmail_attr">On Mon, Jan 15, 2024 at 10:20 AM David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>> wrote:<br></div><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 style="font-family:monospace">Ah, I see what you mean now.</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">In that case, nevermind. I understand now why my idea is backwards incompatible. What a shame.</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Acknowledging that this idea no longer has ground to stand on, let me ask the hypothetical -- would do-while loops be better with this feature implemented? I am not at all trying to get this feature implemented anymore. I just want to know if it would have been a good idea in the first place, backwards compatibility issues aside.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 15, 2024 at 4:04 AM Tagir Valeev <<a href="mailto:amaembo@gmail.com" target="_blank">amaembo@gmail.com</a>> wrote:<br></div><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">E.g.:<div><br></div><div><div style="color:rgb(8,8,8)"><pre style="font-family:"JetBrains Mono",monospace"><span style="color:rgb(0,66,140);font-weight:bold">public class </span><span style="color:rgb(0,0,0)">DoWhile </span>{<br> <span style="color:rgb(0,66,140);font-weight:bold">static boolean </span><span style="color:rgb(0,75,159);font-style:italic">b </span>= <span style="color:rgb(0,66,140);font-weight:bold">false</span>;<br><br> <span style="color:rgb(0,66,140);font-weight:bold">public static void </span><span style="color:rgb(18,19,20)">main</span>(<span style="color:rgb(0,0,0)">String</span>[] <span style="color:rgb(0,0,0)">args</span>) {<br> <span style="color:rgb(0,66,140);font-weight:bold">do </span>{<br> <span style="color:rgb(0,66,140);font-weight:bold">boolean </span><span style="color:rgb(0,0,0)">b </span>= <span style="color:rgb(0,66,140);font-weight:bold">true</span>;<br> } <span style="color:rgb(0,66,140);font-weight:bold">while </span>(<span style="color:rgb(0,75,159);font-style:italic">b</span>);<br> }<br>}</pre></div></div><div><br></div><div>Now, this code is correct and finishes successfully. If we expand the scope of local b to the condition, it will shadow the field b. As a result, the code will still be correct but stuck in endless loop.</div><div><br></div><div>With best regards,</div><div>Tagir Valeev.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 15, 2024 at 9:00 AM David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>> wrote:<br></div><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 style="font-family:monospace">Hello,</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Thank you for your response!</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">I am afraid I do not follow. Could you post an example?</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">From my quick experimentation now, it seems like what you describe isn't possible. </div><div style="font-family:monospace"><br></div><div style="font-family:monospace">The only way I could see what you are saying to be true is if a variable is in scope for the while condition, but not in scope for the do-while body. And I can't seem to create a situation where a variable is in scope for the while condition, but is not in scope for the body.</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Or maybe it is something else entirely? Please lmk.</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Thank you for your time and help!</div><div style="font-family:monospace">David Alayachew<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 15, 2024 at 2:51 AM Tagir Valeev <<a href="mailto:amaembo@gmail.com" target="_blank">amaembo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Note that this is not the compatible change, as the variable may shadow same-named field which was in the scope of condition before.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 15, 2024, 08:49 David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>> wrote:<br></div><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 style="font-family:monospace">Hello Amber Dev Team,</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Would it make sense and would there be value in allowing variables declared in the body of a do-while loop to still be considered in scope for the condition of the same do-while loop?</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Basically, the following example is currently not valid Java, but should it be?</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">```java</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">do</div><div style="font-family:monospace">{</div><div style="font-family:monospace"><br></div><div style="font-family:monospace"> final var someVar = someMethod();</div><div style="font-family:monospace"><br></div><div style="font-family:monospace"> doSomethingWith(someVar);</div><div style="font-family:monospace"><br></div><div style="font-family:monospace"> //etc.<br></div><div style="font-family:monospace"><br></div><div style="font-family:monospace">}</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">while (someVar.someBooleanReturningMethod());<br></div><div style="font-family:monospace"><br></div><div style="font-family:monospace">```</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">I think this is clear, simple, and is better than the alternative.</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">```java</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">/* Must be mutable! :( */</div><div style="font-family:monospace">/* And you probably need a default value too! */<br></div><div style="font-family:monospace">var someVar = someYuckyDefaultOrWorseYetNull;<br></div><div style="font-family:monospace"><br></div><div style="font-family:monospace">do</div><div style="font-family:monospace">{</div><div style="font-family:monospace"><br></div><div style="font-family:monospace"> //code<br></div><div style="font-family:monospace"><br></div><div style="font-family:monospace">}</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">while (someVar != 42);</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">//And annoyingly enough, the variable is still in scope outside of the loop! Blegh.<br></div><div style="font-family:monospace"><br></div><div style="font-family:monospace">```</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Since do-while loops are very old, I wouldn't be surprised if someone else has asked this before. But regardless, I couldn't find anything, so I ask now -- is there any value in doing this?</div><div style="font-family:monospace"><br></div><div style="font-family:monospace">Thank you for your time and help!</div><div style="font-family:monospace">David Alayachew<br></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div></div>