Variadic function call - detect if function was called

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Variadic function call - detect if function was called

08 Oct 2013, 07:40

If a given function is called through a variadic function call and I've made an error by i.e. specifying a wrong parameter / specifying too few parameters, how can I check, if the function was called or not?

Code: Select all

fun(p1, p2, p3) {
	MsgBox, %    "p1=" p1
			. "`np2=" p2
			. "`np3=" p3
}

prms := {"p1" : "a", "p2" : "b", "p3" : "c"}				; not called - how to check
;prms := [], prms.p1 := "a", prms.p2 := "b", prms.p3 := "c"	; not called - how to check
;prms := {"p1" : "a", "p2" : "b", "p3" : "c", 111 : "dummy"}	; called
fun(prms*)
jpginc
Posts: 124
Joined: 29 Sep 2013, 22:35

Re: Variadic function call - detect if function was called

09 Oct 2013, 05:51

I would just add a return statement to the function your going to call like this:

Code: Select all

fun(p1, p2, p3) {
   MsgBox, %    "p1=" p1
         . "`np2=" p2
         . "`np3=" p3
return 1
}

prms := {"p1" : "a", "p2" : "b", "p3" : "c"}            ; not called - how to check
;prms := [], prms.p1 := "a", prms.p2 := "b", prms.p3 := "c"   ; not called - how to check
;prms := {"p1" : "a", "p2" : "b", "p3" : "c", 111 : "dummy"}   ; called

if(fun(prms*) == 1) {
	msgbox, was called
} else
{ 	MsgBox, was not called
}
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Variadic function call - detect if function was called

09 Oct 2013, 16:38

You can take advantage of the following behaviour common to variadic and/or dynamic function calls:
If the function cannot be called due to one of the reasons below, the evaluation of the expression containing the call stops silently and prematurely, which may lead to inconsistent results:
  • Calling a nonexistent function, which can be avoided by using If IsFunc(VarContainingFuncName). Except for built-in functions, the called function's definition must exist explicitly in the script by means such as #Include or a non-dynamic call to a library function.
  • Passing too few parameters, which can be avoided by checking IsFunc()'s return value (which is the number of mandatory parameters plus one). Note: In v1.0.48+, passing too many parameters is tolerated; each extra parameter is fully evaluated (including any calls to functions) and then discarded.

Code: Select all

r := "not called", r := F([1]*)  ; The second assignment doesn't occur.
MsgBox % r
r := "not called", r := F([1,2]*)
MsgBox % r
r := "not called", F([1,2]*), r := "called"  ; Ignoring return value.
MsgBox % r

F(a,b) {
    return 42
}
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Variadic function call - detect if function was called

09 Oct 2013, 17:03

Spoiler
EDIT:
Just seen this
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: Variadic function call - detect if function was called

11 Oct 2013, 09:37

Ahh, so I could use the expression itself to check if the function was called. Thanks.

After a bit of testing, here is a general way to make sure if a function was called:

Code: Select all

	str := "1"
	;str := "2"
	obj := ["aaa", "bbb", "ccc"]
	;obj := ["aaa", "bbb"]
	# := 0, IsFunc("fun" str) ? (fun%str%(obj*), # := 1) : (# := 2)
	if(# = 0)
		MsgBox, % "Too few parameters."
	else if(# = 1)
		MsgBox, % "Function was called."
	else if(# = 2)
		MsgBox, % "Non-existent function."
	 
		fun1(p1, p2, p3) {
		MsgBox, fun1()
	}
IsFunc() first checks, if the function exists (in case we're calling a non-existent function through a dynamic call, which would revert to calling the __Call() meta-function of the base object (and thus only set the return value of the function to an empty string and got the rest of the expression evaluated)). Supposing we're not using the dynamic call to the non-existing function, if the function got too few parameters, # := 2 will never be evaluated.

Edit: for more info, see this thread by Lexikos.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Lpanatt and 321 guests