Page 2 of 3

Re: One Line If Statements

Posted: 04 Mar 2019, 13:27
by nnnik
Thats not possible in AHK v1 as it is a breaking change.

Re: One Line If Statements

Posted: 04 Mar 2019, 13:29
by Raccoon
You keep SAYING it's a breaking change, but I require some sort of mental intellectual exchange to comprehend where you're coming from. Allow me to conclude that it's a breaking change for myself.

parser scans through lines. parser finds "if" statement. parser looks for opening paren. parser waits for closing[nested] parens. parser concludes that conditions have been identified. parser looks for explicit comma following closing paren. parser treats following strings as THEN action.

Code: Select all

if (1 == 1), stuff to do
if ((1 = 1) && 2 = 2), stuff to do
if (1 = 1) || (2 = 2), stuff to do
if ((((1 = 1) || 2 = 2) || 3 = 3) || 4 = 4), stuff to do

Re: One Line If Statements

Posted: 04 Mar 2019, 13:42
by nnnik
A breaking change is when a change occurs in a framework that breaks previously existing behavior.

Code: Select all

if (condition), alsoPartOfCondition
Was possible before and had a specific behavior.
Your suggestion would change the behavior of that code.

I would be okay with making that change if you prove the absence of this feature being in use anywhere.

Re: One Line If Statements

Posted: 04 Mar 2019, 13:48
by Raccoon
Any precedent for adding a rare-risk of a breaking change, and issuing a Request For Complaints to receive input for outrage? And explaining how to fix the break by wrapping parens around the entire if-expression (as it always has been recommended in the documentation)

Re: One Line If Statements

Posted: 05 Mar 2019, 00:15
by jeeswg
For reference: threads discussing a one-line if-statement in AHK v2.
[Wish] One line while loop (similar to if) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=4983
a080: same-line if parsing error - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=33091

Re: One Line If Statements

Posted: 05 Mar 2019, 02:43
by just me
jeeswg wrote:
04 Mar 2019, 09:14
- @just me:
...

Code: Select all

if cond LINSEP action ;advantage: a generally useful feature (for bunching up lines)
...
So 'bunching up lines' is the sole objective? I cannot detect a real advantage (and I wouldn't change the parser to achieve it).

Re: One Line If Statements

Posted: 05 Mar 2019, 03:17
by jeeswg
- A line separator symbol allows you to combine multiple lines into one line.
- This can increase readability/maintainability/brevity, solves every conceivable one-liner issue (if/while/Loop/for, break/continue/return, curly braces etc), and is conceptually simple.
- (Overuse of a line separator could decrease readability, however, the user could simply replace them with line breaks.)
- A line separator would entail the simplest possible change to the parser (or rather, it bypasses it): a pre-parser replaces the line separator symbol with linefeeds.
- The other suggestions involve more complex changes to the parser. Changes to the parser can be fiendishly complicated to achieve, and risk introducing new bugs.

Re: One Line If Statements

Posted: 16 Mar 2019, 21:13
by lexikos
(true, false), msgbox true is a valid expression, wherein the msgbox variable (presumably empty) and true are concatenated and the result is discarded. An If's expression is never required to be enclosed by parentheses; the parentheses are merely part of the expression. For instance, code like if (x < y) && (y < z) is not uncommon.

If expression, action was allowed in some obsolete v2-alpha builds as a substitute for the one-line IfEqual, but I removed it for consistency with the other control flow statements and for other reasons.

Edit after reading second page: Where does the documentation "recommend" enclosing the entire expression in parentheses? If you let me know, I will fix it (the documentation)... ;)

Re: One Line If Statements

Posted: 23 Mar 2019, 08:24
by 0x00
I don't mean to raise a 'settled' matter, but isn't this what ternaries do, so isn't it more reasonable & useful, to, if at all possible just add Return, Continue & Break support within ternaries. Just a thought.

Re: One Line If Statements

Posted: 23 Mar 2019, 08:28
by nnnik
I dont really want to search for flow execution statements within complex 30 if else laddered one line ternaries (that might even be broken up into multiple lines to increase readability).
So I dont think it would be a necessity.

Re: One Line If Statements

Posted: 23 Mar 2019, 08:54
by 0x00
nnnik wrote:
23 Mar 2019, 08:28
Fair enough.

Re: One Line If Statements

Posted: 09 May 2019, 21:40
by jeeswg
- In this test build, a switch statement has been implemented, which in my view negates the need for one-line if-statements, and the need for bunching up multiple lines of code into one line.
Test build - InputHook, long paths, switch-case - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=24&t=63861

- The switch statement is great for doing 'if a then b' one-liners.
- And 'if a then b and break/continue/return' two-liners.
- I'm really happy with the result. Thanks lexikos.

Code: Select all

;IF ELSE

vNum1 := vNum2 := vNum3 := 0
Loop, 10
{
	if (A_Index = 1)
		vNum1++, vNum2++, vNum3++
	else if (A_Index = 3)
	{
		vNum1++, vNum2++, vNum3++
		continue
	}
	else if (A_Index = 5)
	{
		vNum1++, vNum2++, vNum3++
		break
	}
	else if (A_Index = 7)
		vNum1++, vNum2++, vNum3++
}
MsgBox, % vNum1 " " vNum2 " " vNum3

;==================================================

;SWITCH

vNum1 := vNum2 := vNum3 := 0
Loop, 10
{
	switch
	{
		case (A_Index = 1): vNum1++, vNum2++, vNum3++
		case (A_Index = 3): vNum1++, vNum2++, vNum3++
			continue
		case (A_Index = 5): vNum1++, vNum2++, vNum3++
			break
		case (A_Index = 7): vNum1++, vNum2++, vNum3++
	}
}
MsgBox, % vNum1 " " vNum2 " " vNum3

;==================================================

;SWITCH VAR

vNum1 := vNum2 := vNum3 := 0
Loop, 10
{
	switch A_Index
	{
		case 1: vNum1++, vNum2++, vNum3++
		case 3: vNum1++, vNum2++, vNum3++
			continue
		case 5: vNum1++, vNum2++, vNum3++
			break
		case 7: vNum1++, vNum2++, vNum3++
	}
}
MsgBox, % vNum1 " " vNum2 " " vNum3

;==================================================

return

- For reference:
- The switch statement differs from C++ in at least two ways.
- However, I think that this AHK implementation is better than the C++ implementation.
- In AHK, it executes the code for the first matching case only, and then breaks out of the switch.
- In C++, it executes the code for the first matching case, and all code below up till the end of the switch, and only breaks out of the switch if a 'break' is seen.
- In AHK, 'break' breaks out of the loop.
- In C++, 'break' breaks out of the switch.
- The C++ switch statement is demonstrated here:
C++ Programming - YouTube
https://www.youtube.com/watch?v=Rub-JsjMhWY#t=12m01
- This is described like so in the documentation:
Switch
https://autohotkey.com/docs/commands/Switch.htm
Unlike the switch statement found in some other languages, there is no implicit fall-through and Break is not used (except to break out of an enclosing loop).

- Btw, at present, there is not a two-way compatible AHK v1/v2 way to use a computed method name. AHK v1: obj[method](), AHK v2: obj.%method%().
- However, my one library function where I'd want to do this, Acc_Get(), I could use a switch statement instead.

Re: One Line If Statements

Posted: 09 May 2019, 23:27
by jeeswg
Being able to do something like this, outside of a switch statement, by some means, would still be very useful, especially when writing code at speed:

Code: Select all

;before:
if (condition)
{
	ismatch := 1
	break
}

;after:
if (condition)
	ismatch := 1
	, break

Re: One Line If Statements

Posted: 10 May 2019, 19:57
by lexikos
I very much dislike this style.

If you want to "write code at speed", use hotkeys and hotstrings. For instance, I use this:

Code: Select all

+Enter::
BraceTheCode()
{
    ClipSaved := ClipboardAll
    Clipboard := ""
    Send ^c
    ClipWait 0.2
    Code := Clipboard
    if !RegExMatch(Code, "\R$")
    {
        if !RegExMatch(Code, "^\R")
        {
            Clipboard := RegExReplace(Code, "`am)^(?<=`n)(?=.)", "    ")
            Send {{}{Enter}{}}{Up}{End}{Enter}^v
        }
        else
        {
            Clipboard := Code
            Send {Space}{{}{Enter}{}}{Up}{End}^v
        }
    }
    Sleep 200
    Clipboard := ClipSaved
}
This is tailored to my particular setup, with SciTE4AutoHotkey.

Typing if (condition) +{Enter} produces an OTB block with the caret in the centre.

Typing if (condition){Enter}+{Enter} produces a non-OTB block with the caret in the centre.

I don't use this with Visual Studio, since the editor itself is smart about brace formatting. I just type if (condition){Enter}{{}{Enter} and it expands to a full block with the caret in the centre. I think that AHKStudio has similar features.

Re: One Line If Statements

Posted: 10 May 2019, 20:07
by jeeswg
- Thanks for the example.
- Well, at least I know it's unlikely to be implemented, but I do wonder if some convenient solution is possible.

- One use would be to quickly translate Python code.

- One real-world scenario is: I want to temporarily add a MsgBox before a break/continue.
- So that means adding in braces/indentation etc.
- Although, typically, I prefer to copy the condition and place the MsgBox underneath.

Code: Select all

if (condition)
	break

;version 1, not possible
if (condition)
	MsgBox(var)
	, break

;version 2
if (condition)
{
	MsgBox(var)
	break
}

;version 3
if (condition)
	MsgBox(var)
if (condition)
	break

Re: One Line If Statements

Posted: 11 May 2019, 01:35
by lexikos
So that means adding in braces/indentation etc.
Do you even use AutoHotkey? ;)

I forgot to mention, if I select the word "break" and press my hotkey (above), it surrounds the line with braces. Because of SciTE4AutoHotkey's auto-indentation, the braces and command even have proper indentation. (It could be a bit smarter and actually read the current line rather than the selection, but I know how to use my script.)

Re: One Line If Statements

Posted: 11 May 2019, 04:11
by jeeswg
To me, ideally, conceptually, this should be one line, and so braces shouldn't be needed:

Code: Select all

;current:
{
	var := 1
	break
}

;not possible:
	var := 1, break

;not possible:
	var := 1
	, break

Here's some code, for Notepad (or any Edit control), to 'append' break/continue to a one-liner. That is, to add braces/indentation etc. This, will make possible, coding at speed.

Code: Select all

;e.g. before:
;	var := 1

;e.g. after:
;{
;	var := 1
;	break
;}

;to use: make the line you want to wrap, the current line,
;then trigger the hotkey

#IfWinActive ahk_class Notepad
^#b:: ;notepad - insert braces and 'break'
^#c:: ;notepad - insert braces and 'continue'
vCFS := InStr(A_ThisHotkey, "b") ? "break" : "continue"
ControlGet, hCtl, Hwnd,, Edit1, A
ControlGet, vLineNum, CurrentLine,,, % "ahk_id " hCtl
ControlGet, vText, Line, % vLineNum,, % "ahk_id " hCtl
vPos := RegExMatch(vText, "[^`t]")
if (vPos = 1)
	vText := "`t" vText, vPos := 2
;vIndent := StrRept("`t", vPos-2)
vIndent := ""
Loop, % vPos-2
	vIndent .= "`t"
SendInput, {Home}+{End}
vText := vIndent "{`n" vText "`n" vIndent "`t" vCFS "`n" vIndent "}`n"
;SendInput, % "{Text}" vText
vText := StrReplace(vText, "`n", "`r`n")
Control, EditPaste, % vText,, % "ahk_id " hCtl
return
#IfWinActive

Do you even use AutoHotkey? ;)
It's surprised me, that you haven't wanted, from early on: ObjCount, StrRept (which I needed above), InputBox font size, OnHotkey for logging hotkey use, and other friends. Although, you did implement the vast majority of what I wanted, before I joined the forum. Cheers. ;)

Re: One Line If Statements

Posted: 01 Sep 2021, 11:56
by raron
From OP's post:

Code: Select all

If (1 = 1) msgbox true

If (1 = 1), msgbox true

If (1 = 1) { msgbox true }

If (1 = 1) then msgbox true
This!

+ 1 for One Line If Statements!

How can this language with so much implied stuff (?), not have this basic and I'd say very normal form? I just spent a couple of hours figuring out why the H my if (expression) one-liner didn't work
Image

A simple newline at the right place and voilá!

I'll admit I'm no expert on AHK, but I have used it from time to time.

Btw currently using AHK 1.1.33.09. Maybe they'll fix it in v2, if they haven't already? I haven't tried the v2 beta yet.

Re: One Line If Statements

Posted: 01 Sep 2021, 13:04
by SandyClams
if you want a one-line if in v2 you can just use a short-circuit expression to do it.

Code: Select all

(DoMsg) && MsgBox(True)
(SkipMessage) || MsgBox(True)

Re: One Line If Statements

Posted: 03 Sep 2021, 10:56
by safetycar
SandyClams wrote:
01 Sep 2021, 13:04
if you want a one-line if in v2 you can just use a short-circuit expression to do it.

Code: Select all

(DoMsg) && MsgBox(True)
(SkipMessage) || MsgBox(True)
I think that's more clear. Personally I'm thankful that ahk v2 has simplified so much the usage of commas, I wouldn't ever want go back to seeing stuff like If (1 = 1), msgbox true.