One Line If Statements

Propose new features and changes
User avatar
nnnik
Posts: 4256
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

04 Mar 2019, 13:27

Thats not possible in AHK v1 as it is a breaking change.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

04 Mar 2019, 13:29

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
User avatar
nnnik
Posts: 4256
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

04 Mar 2019, 13:42

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.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

04 Mar 2019, 13:48

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)
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

05 Mar 2019, 00:15

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 6486
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: One Line If Statements

05 Mar 2019, 02:43

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).
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

05 Mar 2019, 03:17

- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6610
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: One Line If Statements

16 Mar 2019, 21:13

(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)... ;)
0x00
Posts: 79
Joined: 22 Jan 2019, 13:12

Re: One Line If Statements

23 Mar 2019, 08:24

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.
User avatar
nnnik
Posts: 4256
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

23 Mar 2019, 08:28

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.
Recommends AHK Studio
0x00
Posts: 79
Joined: 22 Jan 2019, 13:12

Re: One Line If Statements

23 Mar 2019, 08:54

nnnik wrote:
23 Mar 2019, 08:28
Fair enough.
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

09 May 2019, 21:40

- 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.
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
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

09 May 2019, 23:27

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6610
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: One Line If Statements

10 May 2019, 19:57

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.
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

10 May 2019, 20:07

- 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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6610
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: One Line If Statements

11 May 2019, 01:35

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.)
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

11 May 2019, 04:11

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. ;)
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 “Wish List”

Who is online

Users browsing this forum: No registered users and 6 guests