Call function from class instance variable

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sTobi
Posts: 6
Joined: 01 Feb 2024, 15:26

Call function from class instance variable

01 Feb 2024, 17:50

How can I call a function stored inside a class instance variable?

Code: Select all

class TestClass {
    x := () => MsgBox("x")
    y := "y"

    z
    {
        get {
            return this.x
        }
        set {
            this.x := value
        }
    }

    f() {
        this.x()
    }
}

a := () => MsgBox("a")
a()

test := TestClass() ;OK
MsgBox(test.y) ;OK
test.x() ;Error: Too many parameters passed to function.
test.z() ;Error: Too many parameters passed to function.
test.f() ;Error: Too many parameters passed to function.
No matter if go directly via the variable, a property or a method, I always get that error.
How do I do that? If I couldn't store functions on variables, then the `a` example wouldn't work, and I can access the `y` variable fine, so I'm at a loss here.
sTobi
Posts: 6
Joined: 01 Feb 2024, 15:26

Re: Call function from class instance variable

01 Feb 2024, 18:57

Ok, I figured that out, I have to pass any parameter

Code: Select all

x := (_) => MsgBox("x")
, then it works.
But how can I define a code block in that format?
I can do multiple simple statements with

Code: Select all

x := (_) => (
MsgBox("x")
MsgBox("x")
)
but nothing with control structures, like e.g.

Code: Select all

x := (_) => {
MsgBox("x")
if (2 > 1) {
	MsgBox("x")
}
}
does not work.

[Mod edit: Topic was approved after the following response.]
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Call function from class instance variable

01 Feb 2024, 19:54

sTobi wrote:
01 Feb 2024, 17:50
How can I call a function stored inside a class instance variable?

Code: Select all

class TestClass {
    x := () => MsgBox("x")
    y := "y"

    z
    {
        get {
            return this.x
        }
        set {
            this.x := value
        }
    }

    f() {
        this.x()
    }
}

a := () => MsgBox("a")
a()

test := TestClass() ;OK
MsgBox(test.y) ;OK
test.x() ;Error: Too many parameters passed to function.
test.z() ;Error: Too many parameters passed to function.
test.f() ;Error: Too many parameters passed to function.
No matter if go directly via the variable, a property or a method, I always get that error.
How do I do that? If I couldn't store functions on variables, then the `a` example wouldn't work, and I can access the `y` variable fine, so I'm at a loss here.

Just like you are except you need to allow for parameters:

Code: Select all

class TestClass {
    x := (*) => MsgBox("x")
    y := "y"

    z
    {
        get {
            return this.x
        }
        set {
            this.x := value
        }
    }

    f() {
        this.x()
    }
}

a := () => MsgBox("a")
a()

test := TestClass() ;OK
MsgBox(test.y) ;OK
test.x() 
test.z() 
test.f() 
x := (*) => MsgBox("x") is getting passed a 'this' parameter.

You can try this and see more:

Code: Select all

class TestClass {
	q := (this, para?, *) => MsgBox(Type(this) '`n' (para ?? 'Unset'))
}

test := TestClass()
test.q()		; this
test.q(15)		; this, para
test.q(17, 52) 	; this, para, second parameter of 52 is ignored by the *
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
sTobi
Posts: 6
Joined: 01 Feb 2024, 15:26

Re: Call function from class instance variable

02 Feb 2024, 06:40

Thanks, as i wrote above I figured that one out for he most part.
Do you have any idea how I can assign a code-block including control structures (if, loops, etc) like in my 2nd post as an anonymous function to a variable?
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Call function from class instance variable

02 Feb 2024, 14:48

sTobi wrote:
02 Feb 2024, 06:40
Do you have any idea how I can assign a code-block including control structures (if, loops, etc) like in my 2nd post as an anonymous function to a variable?

Fat Arrow anonymous functions cannot do loops, but they can do if-statements through a ternary operator.

Code: Select all

class TestClass 
{
	x := (_) => (MsgBox("x"), (2 > 1) ? MsgBox("x") : '')
}

test := TestClass()
test.x()

I am not sure the point of wanting to do a complex fat arrow anonymous function. Fat arrow is mostly just a shorthand syntax for creating a function like ternary operator is for an if-statement. It is designed for fairly simple functions that do not need all the capabilities of normal function syntax.

Why not just do a real function which when in a class is a method.

Code: Select all

Class TestClass
{
	; x := (_) => (MsgBox("x"), (2 > 1) ? MsgBox("x") : '') ; very similar to below
	x()
	{
		MsgBox("x")
		If (2 > 1)
			MsgBox("x")
	}
}

test := TestClass()
test.x()
It is not exactly the same but close.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: gekunfei and 61 guests