Allow variables declared in do-while loop body to be in scope in the condition?
David Alayachew
davidalayachew at gmail.com
Mon Jan 15 09:59:17 UTC 2024
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.
On Mon, Jan 15, 2024 at 4:48 AM Tagir Valeev <amaembo at gmail.com> wrote:
> Probably. By the way, the original piece of code I usually write as:
>
> while(true) {
> final var someVar = someMethod();
> doSomethingWith(someVar);
> //etc.
> if (!someVar.someBooleanReturningMethod()) break;
> }
>
> Not so pretty too, but still allows keeping the scope of someVar limited
> and avoiding splitting the declaration and the assignment.
>
> With best regards,
> Tagir Valeev.
>
> On Mon, Jan 15, 2024 at 10:20 AM David Alayachew <davidalayachew at gmail.com>
> wrote:
>
>> Ah, I see what you mean now.
>>
>> In that case, nevermind. I understand now why my idea is backwards
>> incompatible. What a shame.
>>
>> 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.
>>
>> On Mon, Jan 15, 2024 at 4:04 AM Tagir Valeev <amaembo at gmail.com> wrote:
>>
>>> E.g.:
>>>
>>> public class DoWhile {
>>> static boolean b = false;
>>>
>>> public static void main(String[] args) {
>>> do {
>>> boolean b = true;
>>> } while (b);
>>> }
>>> }
>>>
>>>
>>> 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.
>>>
>>> With best regards,
>>> Tagir Valeev.
>>>
>>> On Mon, Jan 15, 2024 at 9:00 AM David Alayachew <
>>> davidalayachew at gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> Thank you for your response!
>>>>
>>>> I am afraid I do not follow. Could you post an example?
>>>>
>>>> From my quick experimentation now, it seems like what you describe
>>>> isn't possible.
>>>>
>>>> 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.
>>>>
>>>> Or maybe it is something else entirely? Please lmk.
>>>>
>>>> Thank you for your time and help!
>>>> David Alayachew
>>>>
>>>> On Mon, Jan 15, 2024 at 2:51 AM Tagir Valeev <amaembo at gmail.com> wrote:
>>>>
>>>>> 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/0c8bf0dc/attachment-0001.htm>
More information about the amber-dev
mailing list