Page 1 of 1

Allow shorthand "if" with control flow (break/return)

Posted: 02 Feb 2024, 09:36
by WarlordAkamu67
Is it possible to make this available?

Code: Select all

F1:: {
  Loop {
    (A_Index >= 5) ? break : ""
}
  MsgBox("We got out!")
  return
}

; ------ ;

F2:: {
  Loop {
    (A_Index >= 5) ? return : ""
}
}

Re: Allow shorthand "if" with control flow (break/return)

Posted: 03 Feb 2024, 08:21
by RussF
The ternary operator returns values. Break and Return are control statements, not values (unless you make them variables, which I highly don't recommend.)

Russ

Re: Allow shorthand "if" with control flow (break/return)

Posted: 03 Feb 2024, 10:24
by boiler
RussF wrote:
03 Feb 2024, 08:21
…unless you make them variables…
This can’t be done in v2. See the reserved words here.

Re: Allow shorthand "if" with control flow (break/return)

Posted: 03 Feb 2024, 12:39
by iseahound
Yep, I've struggled with single line loops myself, so I remembered that since AutoHotkey has first-class functions, it's possible to "forget" all control statements such as return, break and just invent your own using a formally untyped lambda calculus.

The following example contains a single line Loop statement. Specifically, it runs MsgBox("Hello world!") 3 times. See: viewtopic.php?f=82&t=124744

Code: Select all

#Requires AutoHotkey v2.0

; Define a Z-combinator extended to accept an additional argument to evaluate.
Z := f => (x => f(v => w => x(x)(v)(w)))(x => f(v => w => x(x)(v)(w)))

; The user defined loop statement, which I've called "L" for Loop.
L := g => y => z => (y > 0 ? (z(), g(y - 1)(z)) : 0)

; The single line loop statement is defined as Z(L)( number of loops ) ( The function you'd like to execute)
Z(L)(3)( MsgBox.Bind("Hello world!") )
And the single line version would be: (ChatGPT Explains: https://sl.bing.net/cd7z0YOhQlM)

Code: Select all

#Requires AutoHotkey v2.0
; Show Hello World 3 times!
(f=>(x=>f(v=>w=>x(x)(v)(w)))(x=>f(v=>w=>x(x)(v)(w))))(g=>y=>z=>(y>0?(z(),g(y-1)(z)):0))(3)( MsgBox.Bind("Hello world!") )
It's important to remember how to build things as dictated by the very structure of the world itself, with complete disregard for any appeals to tradition and authority, i.e. the "accepted" way of doing things.

Re: Allow shorthand "if" with control flow (break/return)

Posted: 03 Feb 2024, 14:40
by flyingDman
It's important to remember how to build things as dictated by the very structure of the world itself, with complete disregard for any appeals to tradition and authority, i.e. the "accepted" way of doing things.
wow!!!!!!!!!!! when you need 10 fat arrows in that one line, I guess you're right!. :D