Bug! Switch not work with Expressions!

Report problems with documented functionality
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Bug! Switch not work with Expressions!

21 Oct 2019, 13:43

When executing this code:

Switch
{
Case WinActive("window1"):
a := 1
Case WinActive("window2"):
a := 2
}

The case WinActive("window1") result always true and is always executed cause WinActive("window1") return value 0x0 wich is strangely considered True but is a 0 so must be considered false.
Insted the same code using If has no problems:

If WinActive("window1") {
a := 1
} Else If WinActive("window2") {
a := 2
}

I am using AutoHotkey v1.1.31.01 64 bit on Windows 10 64 bit.
Thanks for any Help!
Last edited by ShadowMaster85 on 25 Oct 2019, 14:58, edited 1 time in total.
swagfag
Posts: 3003
Joined: 11 Jan 2017, 17:59

Re: Bug Switch and WinActive

21 Oct 2019, 15:37

i havent looked into how Switch is supposed to work on v1 or how it's implemented, but it looks like a bug. the same example runs flawlessly on v2 fwiw, so maybe the problem is with expressions

u can force it into a bool with Case !!WinActive("window1"): or use switch true for the time being
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Re: Bug Switch and WinActive

25 Oct 2019, 10:05

Using Switch True the case WinActive("window1") never trigger when is true:

Switch True
{
Case WinActive("window1"):
a := 1
Case WinActive("window2"):
a := 2
}

Using Switch without value and Case !! the code work:

Switch
{
Case !!WinActive("window1"):
a := 1
Case !!WinActive("window2"):
a := 2
}

But it's only a workaround, there is always a bug since the expression is not correctly evalutated (unlike If wich evalutate all this expressions correctly), using the switch normally the case WinActive("window1") result always true and is always executed cause WinActive("window1") return value 0x0 wich is strangely considered True but is a 0 so must be considered false.

When will be available a fix to this bug? Many thanks!
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Re: Bug Switch and WinActive

25 Oct 2019, 14:56

The same bug in these cases:

Switch
{
Case a:
msgbox ff
Default:
msgbox n
}

If value a := 0 or a := False the case a result always true and is always executed, but since the value is False or 0 it must not be executed.
lexikos
Posts: 6653
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Bug! Switch not work with Expressions!

29 Oct 2019, 17:16

This is an indirect product of this v1 quirk:
Note: In AutoHotkey v1, a quoted string (or the result of concatenating with a quoted string) is never considered numeric when used directly in an expression.
Source: Variables and Expressions - Definition & Usage | AutoHotkey
During expression evaluation, quoted strings (and the results of concatenating quoted strings) are identified as SYM_STRING, while (string) values of undetermined type are SYM_OPERAND. The latter includes numeric constants, return values from functions and the result of concatenating (if there are no quoted strings involved). SYM_STRING is always considered non-numeric, and therefore never considered zero; the only false value is "". For example:

Code: Select all

if "0" && 1
	MsgBox True!
Case evaluates its expression and, if it is a string, returns a SYM_STRING value.

It will be fixed.
ShadowMaster85 wrote:Using Switch True the case WinActive("window1") never trigger when is true
It is probably never true (1). WinActive returns a window handle or zero as a hexadecimal string, not a boolean value. If WinActive("window1") = true is false.
If present, SwitchValue is evaluated once and compared to each case value until a match is found, and then that case is executed. Otherwise, the first case which evaluates to true (non-zero and non-empty) is executed.
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Re: Bug! Switch not work with Expressions!

31 Oct 2019, 11:19

lexikos wrote:
ShadowMaster85 wrote:Using Switch True the case WinActive("window1") never trigger when is true
It is probably never true (1). WinActive returns a window handle or zero as a hexadecimal string, not a boolean value. If WinActive("window1") = true is false.
If present, SwitchValue is evaluated once and compared to each case value until a match is found, and then that case is executed. Otherwise, the first case which evaluates to true (non-zero and non-empty) is executed.
When using Switch without value:
Switch
{
Case WinActive("window1"):
msgbox a
}
The Case WinActive("window1") is always executed even if window1 is not active, but when i use the same condition with if: If WinActive("window1") it execute the code contained inside that if only if window1 is active.
Anyway this is always relative to the same bug?

Thanks for your help! :)
lexikos
Posts: 6653
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Bug! Switch not work with Expressions!

31 Oct 2019, 22:40

I already explained that bug.

Your "switch true" example is unrelated, and is not a bug. It is how switch is supposed to work.
sneazzy95
Posts: 2
Joined: 08 Nov 2019, 10:58

Re: Bug! Switch not work with Expressions!

08 Nov 2019, 11:12

Same bug for me too, I thought I was the one so far

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 9 guests