[v2] Check for if var is callable function Topic is solved

Propose new features and changes
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

[v2] Check for if var is callable function

20 Nov 2021, 04:52

For v1, there is a function called Isfunc() allowing to see if a string or function reference is a callable function. However, v2 does not have this function, nor does there exist a "simple" way to see if a string or reference is a callable function without using try and catch.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [v2] Check for if var is callable function

20 Nov 2021, 05:20

It is possible to create a similar function in V2

This is discussed in post viewtopic.php?f=82&t=93150&p=412456&hilit=v2+isfunc#p412456

Code: Select all

#Requires AutoHotkey v2.0-beta.1
IsFunc(FunctionName){
	Try{
		return %FunctionName%.MinParams+1
	}
	Catch{
		return 0
	}
	return
}
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: [v2] Check for if var is callable function

20 Nov 2021, 21:06

I did state that I'd rather this be built-in than to include there anywhere I needed it but in the meantime this could work.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2] Check for if var is callable function  Topic is solved

22 Nov 2021, 03:32

A string is never a callable function.

If you have a function name in a string, and want the function itself (to call or inspect), what you do in v2 is a double-deref to get the value of the variable with that name. %Name%() does exactly that, but then also calls the value; it is not a dynamic function call and does not require the name of a function.

Any number of variables could contain a reference to one function, or to other callable values which are not functions. Given a function's name, it may or may not be possible to get a reference to that function from within the current scope. The function might be defined inside another function, or might be shadowed by local variables.

IsFunc's return value can be used to determine whether you have enough parameter values to call the function, but in v2 you might have too many. To determine whether the function can be called with the number of parameters you will supply, multiple properties of the (potential) function must be checked.


Basically, IsFunc is usually the wrong function for the job. Fortunately we already have HasMethod...
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: [v2] Check for if var is callable function

28 Nov 2021, 15:08

Nice. How have I never noticed this particular usage lol f HasMethod?!?!
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: [v2] Check for if var is callable function

28 Nov 2021, 15:59

In case anyone googles this and isn't clear on the answer, its:

Code: Select all

MsgBox HasMethod(a_function, "Call")
https://lexikos.github.io/v2/docs/objects/Func.htm#Call wrote:The "Call" method is implied when calling a value, so need not be explicitly specified.
SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

Re: [v2] Check for if var is callable function

28 Nov 2021, 16:23

actually (@iseahound), you can just do a HasMethod(a_function), omitting argument 2 checks whether the value at argument 1 is callable

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 18 guests