Something similar to call_user_func_array(PHP)? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Something similar to call_user_func_array(PHP)?

Post by RomAnT » 27 Sep 2021, 11:03

Need to call different functions from another function however this functions have different number of arguments. Is there something like call_user_func_array() function or any other way to achieve this?

Code: Select all

myFunction(inputStr, paramOne := "", paramTwo := "") {
	msgbox inputStr " P1: " paramOne " P2: " paramTwo
}


callerFunct(functName, functArgsArr) {

	myVar := ""
	n := 1
	
	while n <= functArgsArr.length {
		myVar := myVar . " " . functArgsArr[n]
		n++
	}
	msgbox myVar
	
	%functName%("Hello World!", %myVar%)
}

callerFunct("myFunction", ["one", "two"])


RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Re: Something similar to call_user_func_array(PHP)?

Post by RomAnT » 27 Sep 2021, 11:45

swagfag wrote:
27 Sep 2021, 11:28
yea, thats not needed
https://lexikos.github.io/v2/docs/Functions.htm#Variadic
I don't see how serves same purpose, basically what it does it pass arguments to function as array, however for this function needs to be declared this way.

I would not be able to use built-in functions with this method.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Something similar to call_user_func_array(PHP)?

Post by swagfag » 27 Sep 2021, 12:17

read the entire thing

RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Re: Something similar to call_user_func_array(PHP)?

Post by RomAnT » 27 Sep 2021, 12:37

swagfag wrote:
27 Sep 2021, 12:17
read the entire thing
I spent time studying how this solution can not achieve functionality i want because
- functions i want to call will have different number of arguments
- arguments they accept are separate string arguments instead of single array argument
- this will not work for built in functions

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Something similar to call_user_func_array(PHP)?

Post by TheArkive » 27 Sep 2021, 13:42

@RomAnT
MyFunc(params_array*)

You pass the array as a regular var with *. As long as you pass the proper number of params in the array it should work.

Edit:

Code: Select all


arr := ["param1","param2"]
MyFunc(arr*) ; calls MyFunc() with 2 params


RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Re: Something similar to call_user_func_array(PHP)?

Post by RomAnT » 27 Sep 2021, 14:05

TheArkive wrote:
27 Sep 2021, 13:42
You pass the array as a regular var with *. As long as you pass the proper number of params in the array it should work.

Code: Select all


arr := ["param1","param2"]
MyFunc(arr*) ; calls MyFunc() with 2 params


And it does not Work

Code: Select all

myFunction(inputStr, paramOne := "", paramTwo := "") {
	msgbox inputStr " P1: " paramOne " P2: " paramTwo
}


myArr := ["one", "two"]

myFunction("hello" myArr*)   ; <--- Error:  Expected a String but got an Array.

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Something similar to call_user_func_array(PHP)?

Post by kczx3 » 27 Sep 2021, 14:11

You forgot a comma…

RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Re: Something similar to call_user_func_array(PHP)?

Post by RomAnT » 28 Sep 2021, 04:55

Indeed this works! Thanks everyone.

Post Reply

Return to “Ask for Help (v2)”