Page 1 of 1

one line IF command

Posted: 19 Jul 2018, 23:35
by smarq8
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 }

Re: one line IF command

Posted: 20 Jul 2018, 00:07
by guest3456
-1

Re: one line IF command

Posted: 20 Jul 2018, 00:38
by jeeswg
- @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

Re: one line IF command

Posted: 20 Jul 2018, 03:37
by swagfag
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

Re: one line IF command

Posted: 20 Jul 2018, 06:28
by jeeswg
- @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