[v2.0-] Arrow function inconsistency? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

[v2.0-] Arrow function inconsistency?

Post by RaptorX » 27 Nov 2022, 14:06

When using arrow functions, it looks like when using the onEvent method on a GUI you dont need to add parameters when calling functions without arguments. But thats not true when using OnEvent on a Control.

Code: Select all

main := Gui()
main.OnEvent('Close', (*) => ExitApp) ; this works fine

main.Add('Button', '', 'Exits App').OnEvent('Click', (*)=> ExitApp()) ; this works fine too
main.Add('Button', '', 'Does Nothing').OnEvent('Click', (*)=> ExitApp) ; this doesnt work
main.Show
return
Is that expected?
because I would have expected all those lines to have similar behavior.
Projects:
AHK-ToolKit

iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: [v2.0-] Arrow function inconsistency?

Post by iseahound » 27 Nov 2022, 14:32

Yeah, I can see why this would be confusing...

The idea is that you have a function ExitApp that takes zero arguments (has an arity of zero / arity-0 / 0-ary). However, the OnEvent call passes one or more objects to the function. So we could say that there is an arity mismatch (The number of passed arguments is greater than the number of accepted elements)

Another idea is the function call ExitApp() and the function object ExitApp are not the same. So ExitApp would return the object ExitApp, and ExitApp() would return the result of the function call res := ExitApp().

The correct syntax should be (*) => ExitApp().
Spoiler

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [v2.0-] Arrow function inconsistency?  Topic is solved

Post by swagfag » 27 Nov 2022, 15:21

https://lexikos.github.io/v2/docs/Language.htm#function-call-statements
legacy-style "command" invocations are tied to specific conditions, and expecting them to work inside of expression is not one of them
a more interesting question is why main.OnEvent('Close', (*) => ExitApp) "this works fine" (as in: the Gui is automatically hidden, and in turn the script exits on its own since its not Persistent and there isnt anything else around to keep it alive), considering an Object is a truthy value, so returning it should have prevented the automatic hiding of the Gui(and eventual script exit)

iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: [v2.0-] Arrow function inconsistency?

Post by iseahound » 27 Nov 2022, 15:40

I mean it has nothing to do with the syntax but rather the implementation of 'Close' itself. You're probably right it's a truthy value.
test

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2.0-] Arrow function inconsistency?

Post by RaptorX » 27 Nov 2022, 16:06

swagfag wrote:
27 Nov 2022, 15:21
https://lexikos.github.io/v2/docs/Language.htm#function-call-statements
legacy-style "command" invocations are tied to specific conditions, and expecting them to work inside of expression is not one of them
a more interesting question is why main.OnEvent('Close', (*) => ExitApp) "this works fine" (as in: the Gui is automatically hidden, and in turn the script exits on its own since its not Persistent and there isnt anything else around to keep it alive), considering an Object is a truthy value, so returning it should have prevented the automatic hiding of the Gui(and eventual script exit)
Oh okay, so I thought it was working fine but it is merely a coincidence in the sense that the script was just exiting based on other conditions, not because ExitApp was working on that particular situation.

I wonder if using command syntax in a simple arrow function should be considered a warning.
Or probably I could suggest thqby to at least mark it as a warning during the static analysis of the code.
Projects:
AHK-ToolKit

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

Re: [v2.0-] Arrow function inconsistency?

Post by lexikos » 28 Nov 2022, 01:09

A callback can prevent this by returning 1 (or true), which will also prevent any remaining callbacks from being called.
ExitApp != 1
Callback Return Value

If multiple callbacks have been registered for an event, a callback may return a non-empty value to prevent any remaining callbacks from being called.

The return value may have additional meaning for specific events. For example, a Close callback may return a non-zero number (such as true) to prevent the GUI window from closing.

Post Reply

Return to “Ask for Help (v2)”