Page 1 of 1

"Expected a string, but got a function..."

Posted: 09 Apr 2024, 15:06
by kunkel321
I'm still new to fat arrow functions... I want the value of bbAuto to change the instant the checkbox is checked/unchecked. I've run into the error before, but I'm having a hard time understanding what causes it...

Code: Select all

	global bbAuto := 0
	bb.Add('Checkbox', 'x+5 Checked' var, 'Auto Lookup`nin editor').OnEvent, "click" (*) => bbAuto := 1
Thoughts?

(for context if needed, more of the code)
Spoiler
EDIT: Once I posted it, I realized it will never "toggle" the value.... Only will set it to 1. :facepalm:
Still though... What causes the error?

Re: "Expected a string, but got a function..."

Posted: 09 Apr 2024, 16:20
by ntepa
Parentheses can be omitted only if the function is called by itself at the start of a line.
There's also a missing comma between "click" and the fat arrow function. The space between them causes "click" to auto-concatenate with the fat arrow function.
Strings cannot concatenate with functions, so it resulted in the error "Expected a string, but got a function".

Code: Select all

bb.Add('Checkbox', 'x+5 Checked' var, 'Auto Lookup`nin editor').OnEvent("click", (*) => bbAuto := 1)

Re: "Expected a string, but got a function..."

Posted: 11 Apr 2024, 22:16
by lexikos
Even in cases where it is valid to omit the parentheses, it is not valid to write a comma immediately after the function/method name. Comma is used only between parameters or to separate sub-expressions, as in x := 1, y := 2.

Code: Select all

MsgBox, "Invalid"
Global variables being directly assigned by a function must be declared inside the function. bbAuto := 1 assigns to a local variable whose value is never used.

If the code was inside a function, removing global would allow the outer function to create a local variable which would be implicitly captured by the inner function. Replacing global with static would have a similar effect, except that the static variable would be referenced directly and not "captured".

Assigning bb.Auto := 1 would make a property of bb, avoiding all issues of variable scope, but causing a circular reference to the Gui. (cb, *) => cb.Gui.Auto := 1 would make a property of bb without creating a circular reference.