Forced expression after IF

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Forced expression after IF

11 Apr 2018, 16:11

Is it legal to use % after if to force an expression?

See the example below.
  • The 1st method works fine.
  • The 2nd method doesn't work, despite the condition haven't changed. It doesn't work because there is no ( after if.
  • The 3rd method works again, because the issue was fixed by enclosing entire condition inside ().
  • But the 3rd method is ugly and also is inconsistent with forced expressions in commands (msgBox % "Something"). I tried the 4th method - it works, it looks better and is consistent with forced expressions in commands. But I never seen this method before.
So, is it a good idea to use % after if? What you think about?

Code: Select all

; To test this script, uncomment one of these 2 lines:
var1 := "foo", var2 := "bar"
;var3 := "baz"

; And then, uncomment one of these 4 methods:
if (var1 = "foo" && var2 = "bar") || var3 = "baz"
;if var3 = "baz" || (var1 = "foo" && var2 = "bar") ; Illegal, doesn't work
;if (var3 = "baz" || (var1 = "foo" && var2 = "bar"))
;if % var3 = "baz" || (var1 = "foo" && var2 = "bar") ; Works, but I never seen it before
    msgBox % "Success"
Last edited by john_c on 11 Apr 2018, 16:17, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Forced expression after IF

11 Apr 2018, 16:16

- This is interesting. The 4th one wouldn't work in AHK v2.
- In this case I would use the '5th' option, which you didn't include:

Code: Select all

if (var3 = "baz") || (var1 = "foo" && var2 = "bar")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Forced expression after IF

12 Apr 2018, 10:40

It will be very interesting to know what Lexikos and other developers of AHK think about it. Quite strange that this method is not mentioned in the docs.
Last edited by john_c on 14 Apr 2018, 06:49, edited 1 time in total.
guest3456
Posts: 3462
Joined: 09 Oct 2013, 10:31

Re: Forced expression after IF

12 Apr 2018, 10:58

this method isn't mentioned in the docs either:

Code: Select all

var = % "my expression " . var2
just because its not mentioned doesn't mean its not supported.

john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Forced expression after IF

12 Apr 2018, 11:33

guest3456 wrote:var = % "my expression " . var2
Yeah, but it's not much differs from var := "my expression " . var2.

In contrast with it, the current logic of () in if is quite confusing (at least, for a lot of novice users). That's what I mean:

Code: Select all

; Works fine
if (var1 = "foo" && var2 = "bar") || var3 = "baz"

; Will not properly work
if var3 = "baz" || (var1 = "foo" && var2 = "bar")
In such unintuitive case, the if % syntax looks quite elegant, easy to understand and consistent. So, it's much worthy to a user then var = % "my expression " . var2.

(Sometimes my messages looks a bit arrogant for myself. Really, I don't mean any arrogance. It's just a flaw in my English language skills and probably some emotions).
guest3456
Posts: 3462
Joined: 09 Oct 2013, 10:31

Re: Forced expression after IF

12 Apr 2018, 11:46

john_c wrote: In contrast with it, the current logic of () in if is quite confusing (at least, for a lot of novice users). That's what I mean:

Code: Select all

; Works fine
if (var1 = "foo" && var2 = "bar") || var3 = "baz"

; Will not properly work
if var3 = "baz" || (var1 = "foo" && var2 = "bar")
In such unintuitive case, the if % syntax looks quite elegant, easy to understand and consistent. So, it's much worthy to a user then var = % "my expression " . var2.

(Sometimes my messages looks a bit arrogant for myself. Really, I don't mean any arrogance. It's just a flaw in my English language skills and probably some emotions).
its not confusing, you've already quoted the docs which explain it. after the if, you need either an open parenthesis or a function call, to force if-expression. thats it. so just use the parens like you should. i dont think "if %" is elegant at all.


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 246 guests