Fat-arrow function after auto-exec part Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
paveld
Posts: 9
Joined: 01 Oct 2020, 05:28

Fat-arrow function after auto-exec part

25 Apr 2024, 04:09

Hello!

Code: Select all

#Requires AutoHotkey v2.0-rc.2 64-bit
#SingleInstance Force
return

; this doesnt work
:x:f::f("WORLD")

; this does work
:x:g::g("WORLD")

; you get a WARNING at this line
f := a => MsgBox("Hello " a)

g(a) {
    MsgBox("Howdy " a)
}

maybe a silly question, but fat-arrow function (in the code it is function `f`) cannot be defined in the open code, but a regular function (such as my function `g`) can? Thanks!!
User avatar
boiler
Posts: 17127
Joined: 21 Dec 2014, 02:44

Re: Fat-arrow function after auto-exec part

25 Apr 2024, 05:03

Put the line where you assign f where it actually executes (above the return) and it will work fine. That exact info is in the warning message itself, by the way.

The function definition occurs as soon as the script runs, but the assignment of the function reference only occurs if/when you execute the line that assigns it.
paveld
Posts: 9
Joined: 01 Oct 2020, 05:28

Re: Fat-arrow function after auto-exec part

25 Apr 2024, 05:11

yes, it would work fine then, but i should have been clearer in my question: why is the definition of a regular function captured after the auto-exec part, but not of a fat-arrow function ?

for me, this is a detail, i am just wondering if it is an expected behavior.
User avatar
boiler
Posts: 17127
Joined: 21 Dec 2014, 02:44

Re: Fat-arrow function after auto-exec part  Topic is solved

25 Apr 2024, 05:22

Both functions are defined as soon as the script runs, but assigning a function reference to a variable only happens if you execute the line that does so, and your script would never execute that line. That is fully expected as that very concept is explained in the documentation of the fat-arrow function.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww, robinson and 32 guests