[Solved] Override __Call() when there is no key()? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kinnex
Posts: 22
Joined: 09 Jun 2016, 07:52

[Solved] Override __Call() when there is no key()?

21 Jun 2016, 10:13

If I understand things correctly, whenever you call a method of a class __Call() is invoked.
This recursively searches the object (and its base classes) for the key that is the function name.
If that key exists, call the function.
Else return ""

Is there example code of the default __Call() method?
Can you override __Call() to invoke a specific function when there is no method passed?

For example:
%myClassInstance%() ; calls __Call() [with key="" ?] -- how do I make this call myClassInstance.DoStuff() ?

In my particular case, I am making a wrapper class to make binding sub routines, functions, and class methods all use one syntax.
So it makes sense that %myClassInstance%() would simply call the label / function / class method exactly like calling a funcObject.
Last edited by kinnex on 21 Jun 2016, 12:14, edited 1 time in total.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Override __Call() when there is no key()?  Topic is solved

21 Jun 2016, 10:31

Code: Select all

instance := new Functor()
%instance%()
return

class Functor
{
	__Call(method, args*)
	{
		if (IsObject(method)) ; if class object is used as method(usually achieved when nesting)
			return this.Call(method, args*)
		if (method == "") ; %instance%() syntax
			return this.Call(args*)
	}

	Call(args*)
	{
		MsgBox Hello
	}
}
I would usually extend from a base class that implements __Call() above and override Call() in the subclass. For reference: Functor
kinnex
Posts: 22
Joined: 09 Jun 2016, 07:52

Re: Override __Call() when there is no key()?

21 Jun 2016, 12:13

Thank you, that is exactly what I needed to do.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Solved] Override __Call() when there is no key()?

21 Jun 2016, 16:32

I actually updated that documentation a few days ago, but only uploaded it just now (you may need to refresh). The original Objects as Functions section hadn't been updated until then. I basically just defined a class using the recommendations on the Function Objects page, updated two examples to use it and moved one from the former page to the latter.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 237 guests