Forced expression after IF Topic is solved

Discuss the future of the AutoHotkey language
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Forced expression after IF

11 Apr 2018, 16:58

As I discovered today, in AHK v1 we can 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.

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"
However, it seems it doesn't work in current release of AHK v2. Will it work in it's future releases?
Last edited by john_c on 12 Apr 2018, 12:01, edited 2 times in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Forced expression after IF

11 Apr 2018, 17:12

I think if is the only place you can't force expression, output variables aside. In v2 you can't % force expression, expressions are the only option. And no it will not be added.

Cheers.

Edit:
expressions wrote:In [v1.1.21+], this prefix can be used in the InputVar parameters of all commands except the traditional IF commands (use If (expression) instead).
Last edited by Helgef on 11 Apr 2018, 17:26, edited 1 time in total.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Forced expression after IF

11 Apr 2018, 17:16

I hope not.
I always recommend using parentheses.
https://softwareengineering.stackexchan ... -necessary
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Forced expression after IF

11 Apr 2018, 17:32

@Flipeador I don't agree here. Parentheses in the described example doesn't change the logic of the condition. They are used just to say "hey, it's not regular if, it's if-expression". It's just weird and unintuitive.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Forced expression after IF

11 Apr 2018, 18:09

Disagree. Parenthesis are how 95% of most programmers would do it.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Forced expression after IF

11 Apr 2018, 18:18

I think the important thing is not that so many programmers use it or not, but what kind of programmers (experts. beginners?). When you are a beginner, you will surely use bad practices. In my case, learning C++ (I still have a lot to learn) helped me a lot to correct my bad programming practices.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Forced expression after IF

11 Apr 2018, 18:40

@Flipeador We speak about different things, sorry.

Code: Select all

; This example is treated as expression, because there is open ( right after the if
if (var1 = "foo" && var2 = "bar") || var3 = "baz"

; This example doesn't treated as expression, for the same reason
if var3 = "baz" || (var1 = "foo" && var2 = "bar")
There is no logic behind this.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Forced expression after IF  Topic is solved

11 Apr 2018, 18:50

- This works in AHK v2, but not in AHK v1:

Code: Select all

var3 := "baz"
if var3 = "baz" || (var1 = "foo" && var2 = "bar")
	MsgBox("y")
- The reason it doesn't work in AHK v1 relates to there being two syntaxes in AHK v1, traditional-style and expression-style. In AHK v1, starting with an opening parenthesis, if (, made sure that the 'if' line was interpreted as expression-style.
- Forcing an expression, by using %, only existed in AHK v1 because there were two syntaxes. AHK v2 aims to tidy up the language, and has gotten rid of 'force an expression'.
- AFAICS the best two-way compatible solution is my '5th' solution, mentioned on your other thread:
Forced expression after IF - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13#p212013

Code: Select all

if (var3 = "baz") || (var1 = "foo" && var2 = "bar")
- This is also two-way compatible, but I would add parentheses:

Code: Select all

if (var1 = "foo" && var2 = "bar") || var3 = "baz"
;to:
if (var1 = "foo" && var2 = "bar") || (var3 = "baz")
[EDIT:] 4000th post!
Last edited by jeeswg on 11 Apr 2018, 20:11, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Forced expression after IF

11 Apr 2018, 19:04

I think I'm going to develop a Chrome add-on that hides all the topics that include two-way compatible stuff. :lol:
I do not understand that obsession of making both versions work in the same Script. Just publish two versions of your code, then it will be clear, reliable and faster.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Forced expression after IF

11 Apr 2018, 19:12

What do you mean 'just' publish two versions. Rule number one of programming is: avoid maintaining two versions of the same thing.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Forced expression after IF

11 Apr 2018, 23:21

john_c wrote:However, it seems it doesn't work in current release of AHK v2.
it doens't work because in v2 all if's are expression-ifs
Flipeador wrote:
john_c wrote:Will it work in it's future releases?
I hope not.
me too
jeeswg wrote:What do you mean 'just' publish two versions. Rule number one of programming is: avoid maintaining two versions of the same thing.
that's not rule #1 of programming. plenty plenty of projects maintain multiple versions. Windows itself still maintains Win7 and puts out bugfixes and security releases. its especially natural when a platform upgrades to a new version to have multiple versions of libraries or plugins. see wordpress or drupal.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Forced expression after IF

11 Apr 2018, 23:27

- I used the word 'avoid', it can't always be avoided.
- The idea of maintaining multiple separate Windows versions is a paradigm, it is not ideal, and is a typical problem of commercial software.
- If you have to maintain multiple versions, then, as far as possible, it's good to have components that only have one version.
- What's your number one rule of programming?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Forced expression after IF

11 Apr 2018, 23:53

guest3456 wrote:
jeeswg wrote:What do you mean 'just' publish two versions. Rule number one of programming is: avoid maintaining two versions of the same thing.
that's not rule #1 of programming. plenty plenty of projects maintain multiple versions. Windows itself still maintains Win7 and puts out bugfixes and security releases. its especially natural when a platform upgrades to a new version to have multiple versions of libraries or plugins. see wordpress or drupal.
In the case of Windows or alike Im fairly certain that they have methods to work on one version which outputs for several different versions.
Also plugins are not part of the original Wordpress - they are modules that can be loaded into Wordpress.
What jeeswg means is to avoid having several scripts of the same script lying around and as a single programmer thats more than correct.
Recommends AHK Studio
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Forced expression after IF

12 Apr 2018, 03:23

jeeswg wrote:Forcing an expression, by using %, only existed in AHK v1 because there were two syntaxes. AHK v2 aims to tidy up the language, and has gotten rid of 'force an expression'.
Now I understand that my idea was really bad. Marked jeeswg's post as "answer".
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Forced expression after IF

12 Apr 2018, 09:02

nnnik wrote:In the case of Windows or alike Im fairly certain that they have methods to work on one version which outputs for several different versions.
Also plugins are not part of the original Wordpress - they are modules that can be loaded into Wordpress.
What jeeswg means is to avoid having several scripts of the same script lying around and as a single programmer thats more than correct.
the frameworks themselves of wordpress and drupal maintain older versions for bug and security fixes as well. i'm just saying when v2's main purpose is for changes that BREAK compatibility with syntax changes, its not farfeteched at all to think you might have to maintain two libraries. of course if you can avoid it, you try to, like we've done with the v2-Gdip

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Forced expression after IF

12 Apr 2018, 14:30

- Even Microsoft have changed their minds about the separate version paradigm.
Microsoft to stop producing Windows versions - BBC News
http://www.bbc.co.uk/news/technology-32658340
- If they did indeed do this, this would be the biggest change ever made to the company. So perhaps it is true that:
Rule number one of programming is: avoid maintaining two versions of the same thing.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Forced expression after IF

13 Apr 2018, 22:05

Rule number one of programming is: avoid maintaining two versions of the same thing.
I do not think it applies to this. We are talking about some simple class or function (in ahk). I'm not saying it's 100% good, but it's definitely better than trying to make two different major versions work in the same script. I do not think it necessary to have to mention why. Anyway, in my case it does not apply, I only use v2; although it is true that if I have to help I have no choice but to write for v1 (for the moment).
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Forced expression after IF

14 Apr 2018, 02:17

If I had followed the rule "avoid maintaining two versions of the same thing" since 2011, would you still be trying to write "two-way compatible" scripts?
Spoiler
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Forced expression after IF

14 Apr 2018, 02:39

Re: Two-way compatibility

If you think you'll convert your AHK 1.1 scripts to v2 some day, it's a good idea to avoid things known as not supported by v2. In this case, it's very simple not to 'force an expression' in AHK 1.1. That's why I recommend this kind of 'two-way compatibility'. But I think it's useless to write massive function libraries to make AHK 1.1 'compatible' with AHK v2.
jeeswg wrote:[EDIT:] 4000th post!
Still counting posts? :roll:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Forced expression after IF

14 Apr 2018, 02:44

- @lexikos: I did not have AHK v2 in mind when I made my point.
- I suppose that if the approach had been to complete AHK v1, and then remove some syntax to give AHK v2, I would never have had any two-way compatibility issues. However, there may have been good reasons to work on both simultaneously that I don't know about. Another key principle is that sometimes it's better to start again from scratch, rather than try to amend something.
- The work to make two-way compatible scripts doable has been less than the work to maintain two versions of everything. The only current obstacle is loops, although even for that, ugly solutions exist at present. However, if AHK v1 loops could handle expression-style parameters now, perhaps via a directive, that would be good for AHK v1 anyhow.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 33 guests