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.
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.
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().
; Incorrect
; fn(*) => ExitApp ; This returns the ExitApp function object.
fn(*) {
return ExitApp ; This returns the ExitApp function object. Does not execute.
}
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)
main := Gui()
main.OnEvent('Close', (*) => msgbox) ; this doesn't work
main.addbutton()
main.show()
main := Gui()
main.OnEvent('Close', (*) => msgbox()) ; this works fine
main.addbutton()
main.show()
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.
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.