Why the interpreter complains about multiple statements inside an arrow function?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
slishnevsky
Posts: 26
Joined: 07 Mar 2024, 06:50

Why the interpreter complains about multiple statements inside an arrow function?

Post by slishnevsky » 26 Mar 2024, 12:06

Hello.

I was wondering why the interpreter complains about multiple statements inside an arrow function:

Code: Select all

  SetTimer(() => {
    color := PixelGetColor(270, 360)
    ShowMessageBox('Uploading "' filename '"...')
  }, 1000)
image.png
image.png (12.98 KiB) Viewed 121 times

vmech
Posts: 356
Joined: 25 Aug 2019, 13:03

Re: Why the interpreter complains about multiple statements inside an arrow function?

Post by vmech » 26 Mar 2024, 13:02

@slishnevsky
Because braces are not allowed for fat arrow function definition. Use parentheses instead. And separate expressions inside with commas.
Please post your script code inside [code] ... [/code] block. Thank you.

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Why the interpreter complains about multiple statements inside an arrow function?

Post by lexikos » 28 Mar 2024, 19:51

Arrow functions require an expression. You cannot use a statement inside an expression.

Of course, color := PixelGetColor(270, 360) and ShowMessageBox('Uploading "' filename '"...') are also valid expressions, so you can just follow vmech's advice. If you need a control flow statement, you can't use an arrow function.

However, v2.1-alpha.3+ permits function definition expressions, where you just write a proper function definition inline (without =>, and optionally omitting the name).

william_ahk
Posts: 496
Joined: 03 Dec 2018, 20:02

Re: Why the interpreter complains about multiple statements inside an arrow function?

Post by william_ahk » 28 Mar 2024, 22:01

lexikos wrote:
28 Mar 2024, 19:51
However, v2.1-alpha.3+ permits function definition expressions, where you just write a proper function definition inline (without =>, and optionally omitting the name).
That's fantastic! :clap:

Post Reply

Return to “Ask for Help (v2)”