[v2.0.2] Possible if...else bug

Report problems with documented functionality
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

[v2.0.2] Possible if...else bug

15 Jan 2023, 02:52

The following doesn't run, causing an Unexpected "Else" error:

Code: Select all

if 1
    try MsgBox({})
else
    try MsgBox({})
This works:

Code: Select all

if 1 {
    try MsgBox({})
} else
    try MsgBox({})
This also works:

Code: Select all

if 1
    try MsgBox({})
Since for Try...Catch an "Else" can optionally be used after "Catch", and here there is no "Catch", shouldn't it be interpreted to be paired with the "If"?
white-ah
Posts: 8
Joined: 11 Jan 2023, 09:47

Re: [v2.0.2] Possible if...else bug

15 Jan 2023, 04:14

Confirmed, the "else" doesn't terminate the "try" if there is no "catch".
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [v2.0.2] Possible if...else bug

16 Jan 2023, 11:12

but what should be the correct behavior?
try-else could be a valid construct - "execute statements. if they didnt throw, execute more statements unguarded"
so can try-else-finally - "execute statements. if they didnt throw, execute more statements unguarded, before ultimately cleaning up"
although, if this were supported, i could see how this could lead to many a surprised pikachu faces in codebases that majorly rely on single line statements
python can get away with repurposing else for this due to indentation, theres no ambiguity as to which parts the elses belong to. perhaps a different keyword should have been used in ahk instead. either that or forbid this construct altogether
neogna2
Posts: 617
Joined: 15 Sep 2016, 15:44

Re: [v2.0.2] Possible if...else bug

16 Jan 2023, 15:05

swagfag wrote:
16 Jan 2023, 11:12
but what should be the correct behavior?
try-else could be a valid construct - "execute statements. if they didnt throw, execute more statements unguarded"
Alternatively something like If Try or IfTry for cases where we have no need to do something with the error. There seems to be something similar in Swift and Rust according to this. Or, to take it even further, a Try() that returns true/false and can be used inside expressions.
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: [v2.0.2] Possible if...else bug

16 Jan 2023, 15:20

@swagfag, yeah, instead of try-else something like try-catch-then-finally / try-then would cause less ambiguity. That is if try-else were actually supported, which it isn't at the moment. In v3 perhaps? ;)

@neogna2 Try() would be pretty nice to have. It would solve the nasty case of

Code: Select all

try a
catch {
    try b
    catch {
        try c 
        catch {
        }
    }
}
white-ah
Posts: 8
Joined: 11 Jan 2023, 09:47

Re: [v2.0.2] Possible if...else bug

16 Jan 2023, 17:55

The help states:
Every use of an Else must belong to (be associated with) an If statement, Catch, For, Loop or While statement above it. An Else always belongs to the nearest applicable unclaimed statement above it unless a block is used to change that behavior.
and:
Else cannot belong to a Try statement (only a Catch statement), therefor the Else should terminate the Try statement and belong to the nearest applicable unclaimed statement. But apparently the Try statement instead of the Catch statement, claims the Else.

The second code below should work, but doesn't:

Code: Select all

if false 
	try 
		sleep 50
msgbox "base level"

Code: Select all

if false
	try 
		sleep 50
else
	msgbox "else if"

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 38 guests