[v2] Check for if var is callable function Topic is solved
- Delta Pythagorean
- Posts: 628
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
[v2] Check for if var is callable function
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
Re: [v2] Check for if var is callable function
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
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
}
- Delta Pythagorean
- Posts: 628
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Re: [v2] Check for if var is callable function
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
Re: [v2] Check for if var is callable function Topic is solved
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...
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
Nice. How have I never noticed this particular usage lol f HasMethod?!?!
Re: [v2] Check for if var is callable function
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.
-
- Posts: 63
- Joined: 02 Jul 2020, 11:55
Re: [v2] Check for if var is callable function
actually (@iseahound), you can just do a HasMethod(a_function), omitting argument 2 checks whether the value at argument 1 is callable