one line IF command

Propose new features and changes
smarq8
Posts: 51
Joined: 16 Jan 2016, 00:33

one line IF command

19 Jul 2018, 23:35

It would be great if I can use command at the same line as my if statement the same as in c/++
for example

Code: Select all

test := 1 if (test) msgbox or if (test) return <expression> or if (test) { <command1>; <command2>; <command3>; return }
guest3456
Posts: 2431
Joined: 09 Oct 2013, 10:31

Re: one line IF command

20 Jul 2018, 00:07

-1

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

Re: one line IF command

20 Jul 2018, 00:38

- @guest3456: -1 to the syntax or -1 to the principle?
- smarq8: I think the idea is great, the main issues are: what syntax to use (what operator e.g. a symbol or 'then') and whether it's possible to handle break/continue/return (e.g. as functions, or whether AHK could run a text macro internally).
- AFAIK C++ rigidly requires the condition be within parentheses, thus such syntax is possible in C++, with AHK the syntax on an if line is more flexible so probably an operator would be the best solution (so that AHK would know that the if statement had ended).
- For reference, here are some current workaround ideas:

Code: Select all

;SCENARIO 1 (test) && msgbox() (test) ? msgbox() : 0 ;SCENARIO 2 (text) && exit() (text) ? exit() : 0 ret := 0 (text) && (ret := 1) (text) ? (ret := 1) : 0 ;lower down after multiple 'if' one-liners if ret return ret ;also: a big ternary operator could be used ;SCENARIO 3 (test) && (func1(), func2(), func3(), exit()) (test) ? (func1(), func2(), func3(), exit()) : 0 if (test) return format(retstr, func1(), func2(), func3()) if (test) return format("", func1(), func2(), func3()) retstr if (test) return format(retnum, func1(), func2(), func3()) if (test) return format(0, func1(), func2(), func3()) + retnum ret := 0 (test) ? (func1(), func2(), func3(), ret := 1) : 0 (test) && (func1(), func2(), func3(), ret := 1) ;lower down after multiple 'if' one-liners if ret return
homepage | tutorials
[code boxes are currently not working 100%]
[click the 'Reply with quote' button on a post to see the full indented text]
swagfag
Posts: 1365
Joined: 11 Jan 2017, 17:59

Re: one line IF command

20 Jul 2018, 03:37

in v1 i sometimes use:

Code: Select all

<condition> ? <expression>
u cant do that in v2 and have to explicitly add the else branch, i wish u didnt have to:

Code: Select all

<condition> ? <expression> : ""
u can stuff multiple functions in an expression separated by commas as demonstrated by jeeswg. whether u should is a different matter entirely
User avatar
jeeswg
Posts: 5260
Joined: 19 Dec 2016, 01:58
Location: UK

Re: one line IF command

20 Jul 2018, 06:28

- @swagfag: Yes, depending on the situation, I try to find the most readable workaround, and sometimes I go longhand and write everything in full.
- Here are some implementation ideas:

Code: Select all

;worst (current situation): if a { b(), c(), d() return } else if e { f(), g(), h() return } else if i { j(), k(), l() return } ;================================================== ;best (something like this): if a then b(), c(), d(), return else if e then f(), g(), h(), return else if i then j(), k(), l(), return ;================================================== ;halfway house: if a b(), c(), d() , return else if e f(), g(), h() , return else if i j(), k(), l() , return
homepage | tutorials
[code boxes are currently not working 100%]
[click the 'Reply with quote' button on a post to see the full indented text]

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 7 guests