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

Propose new features and changes
User avatar
WarlordAkamu67
Posts: 279
Joined: 21 Mar 2023, 06:52

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

02 Feb 2024, 09:36

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 : ""
}
}
The End.
RussF
Posts: 1454
Joined: 05 Aug 2021, 06:36

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

03 Feb 2024, 08:21

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
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

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

03 Feb 2024, 10:24

RussF wrote:
03 Feb 2024, 08:21
…unless you make them variables…
This can’t be done in v2. See the reserved words here.
iseahound
Posts: 1582
Joined: 13 Aug 2016, 21:04
Contact:

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

03 Feb 2024, 12:39

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.
User avatar
flyingDman
Posts: 3019
Joined: 29 Sep 2013, 19:01

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

03 Feb 2024, 14:40

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
14.3 & 1.3.7

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 15 guests