Allow variables declared in do-while loop body to be in scope in the condition?
Tagir Valeev
amaembo at gmail.com
Mon Jan 15 07:51:13 UTC 2024
Note that this is not the compatible change, as the variable may shadow
same-named field which was in the scope of condition before.
On Mon, Jan 15, 2024, 08:49 David Alayachew <davidalayachew at gmail.com>
wrote:
> Hello Amber Dev Team,
>
> 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?
>
> Basically, the following example is currently not valid Java, but should
> it be?
>
> ```java
>
> do
> {
>
> final var someVar = someMethod();
>
> doSomethingWith(someVar);
>
> //etc.
>
> }
>
> while (someVar.someBooleanReturningMethod());
>
> ```
>
> I think this is clear, simple, and is better than the alternative.
>
> ```java
>
> /* Must be mutable! :( */
> /* And you probably need a default value too! */
> var someVar = someYuckyDefaultOrWorseYetNull;
>
> do
> {
>
> //code
>
> }
>
> while (someVar != 42);
>
> //And annoyingly enough, the variable is still in scope outside of the
> loop! Blegh.
>
> ```
>
> 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?
>
> Thank you for your time and help!
> David Alayachew
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240115/52720b14/attachment.htm>
More information about the amber-dev
mailing list