Page 1 of 1

Fat-arrow function after auto-exec part

Posted: 25 Apr 2024, 04:09
by paveld
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!!

Re: Fat-arrow function after auto-exec part

Posted: 25 Apr 2024, 05:03
by boiler
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.

Re: Fat-arrow function after auto-exec part

Posted: 25 Apr 2024, 05:11
by paveld
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.

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

Posted: 25 Apr 2024, 05:22
by boiler
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.

Re: Fat-arrow function after auto-exec part

Posted: 25 Apr 2024, 05:27
by paveld
@boiler thank you!