Page 1 of 1

Help with function references

Posted: 03 Dec 2018, 01:38
by Revvilo
Hi there! I'm having some issues using function references in the below code.
I've condensed it down into a small readable script, therefore this won't have much context, but it seems that if I want to call a function via reference using .Call(), I have to pass an argument, or it simply won't execute the function. Why is that?
If this script is run, the first call will show the messagebox, but the second one won't, even though an argument isn't required by the function.

Code: Select all

Class TestClass {
    TestFunction() {
        Msgbox, Function Triggered
    }
}

fnc := Func("TestClass.TestFunction")

fnc.Call(test) ; Calls and shows messagebox
fnc.Call() ; Doesn't seem to call at all
I'm sure it's probably an issue on my end, but I just can't wrap my head around it. Would really appreciate any input.

Re: Help with function references  Topic is solved

Posted: 03 Dec 2018, 01:53
by swagfag
You need to use ObjBindMethod to create function references to class methods.

apparently not, see helgef's comment below

Code: Select all

Class TestClass {
    TestFunction(msg) {
        Msgbox % msg
    }
}

fnc := TestClass.TestFunction
fnc.Call("", "Function Triggered")

Re: Help with function references

Posted: 03 Dec 2018, 02:14
by Revvilo
You're a life saver. Thank you!

Re: Help with function references

Posted: 03 Dec 2018, 02:17
by Helgef
You do not need to use objbindmethod. Methods are stored by reference in the class object. You can obtain a reference directly via fnc := TestClass.TestFunction, this is recommended over using func. The issue you have is that all methods have a hidden this parameter which needs to be passed. You can read about it in the objects page in the documentation.

Cheers.

Re: Help with function references

Posted: 04 Dec 2018, 11:56
by Helgef
Also note that the first parameter in fnc.Call("", "Function Triggered") is the hidden this parameter, visual example,

Code: Select all

Class TestClass {
    TestFunction(msg) {
        Msgbox % this "`n" msg
    }
}

fnc := TestClass.TestFunction
fnc.Call("abc", "Function Triggered")

Re: Help with function references

Posted: 05 Dec 2018, 00:20
by swagfag
the only real shame is u cant use this method to pass the fn reference to a timer since it expects a bound function object, but, oh well, v1 woes

Re: Help with function references

Posted: 05 Dec 2018, 00:47
by wolf_II
:?:

Code: Select all

#Persistent

    fnc := TestClass.Timer.Bind(TestClass)
    SetTimer, %fnc%, 2000
    
return ; end of auto-execute section

class TestClass {
    Timer() {
        Msgbox,,, Function Triggered, 1
    }
}

Re: Help with function references

Posted: 05 Dec 2018, 03:27
by Helgef
It is not that settimer expects a BoundFunc, you can pass a Func as long as its minparams is 0. Methods defined within a class body always have minparams > 0. You can also implement call,

Code: Select all

#Persistent

	SetTimer, %TestClass%, 2000
    
return ; end of auto-execute section

class TestClass {
	Timer() {
		Msgbox,,, Function Triggered, 1
	}
	call(){
		this.timer()
	}
}
This will pass TestClass to the hidden this parameter of the call method.
Hi wolf_II :wave: