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?

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 130 times
vmech
Posts: 357
Joined: 25 Aug 2019, 13:03

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

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?

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?

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:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 23 guests