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

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

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

09 Apr 2024, 15:06

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?
ste(phen|ve) kunkel
ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

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

09 Apr 2024, 16:20

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)
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

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

11 Apr 2024, 22:16

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.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Datrik, lukepker, mikeyww, vmech and 75 guests