Page 1 of 1
[v2] Check for if var is callable function
Posted: 20 Nov 2021, 04:52
by Delta Pythagorean
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.
Re: [v2] Check for if var is callable function
Posted: 20 Nov 2021, 05:20
by AHK_user
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
}
Re: [v2] Check for if var is callable function
Posted: 20 Nov 2021, 21:06
by Delta Pythagorean
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.
Re: [v2] Check for if var is callable function Topic is solved
Posted: 22 Nov 2021, 03:32
by lexikos
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...
Re: [v2] Check for if var is callable function
Posted: 28 Nov 2021, 15:08
by kczx3
Nice. How have I never noticed this particular usage lol f HasMethod?!?!
Re: [v2] Check for if var is callable function
Posted: 28 Nov 2021, 15:59
by iseahound
In case anyone googles this and isn't clear on the answer, its:
Code: Select all
MsgBox HasMethod(a_function, "Call")
Re: [v2] Check for if var is callable function
Posted: 28 Nov 2021, 16:23
by SandyClams
actually (@iseahound), you can just do a HasMethod(a_function), omitting argument 2 checks whether the value at argument 1 is callable