Expand string parameter list

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Expand string parameter list

17 Mar 2021, 13:59

What would be the easiest way to expand a string that represents parameters to a function? The function, number of parameters, and parameter types are all unknown - but they are all static data (strings and numbers).

Here's an example of what I mean. My strings may look like this:

Code: Select all

myfunc := "CallThis"
myargs := "1,two,three,4,5.0"
I'm currently using StrSplit(), then returning a closure that calls the function with the exact number of parameters by using a switch statement (each case of the switch has a different number of parameters). But that solution is pretty ugly, and requires manually expanding the switch to support any specific number of parameters. Is there any quick and easy way to bind this string of parameters to the function call as a closure or bound function?

I tried just expanding the entire string: return %myfunc%.Bind(%myargs%), but AHK didn't seem to like that very much. I think because it is expanding the string contents into would-be variables instead of data - it doesn't know if my parameters (such as two and three) are variables or static strings. They are all static strings, in case that matters.

I appreciate any advice!
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Expand string parameter list

17 Mar 2021, 14:18

Wow, I think I just accidentally stumbled onto exactly what I was looking for. Looks like syntax array* will auto-expand the array into a function's parameter list. So I'm assuming I can just do this: return %myfunc%.Bind(StrSplit(myargs,",")*)

If anyone has any experience with this feature or situation, please feel free to share your wisdom. Does this work the way it appears to be working?

One other question - just out of curiosity, is there any way to call on a variable within the array of values? Say, for example, if I have a global variable already defined, would there be any way to send that global value as one of the parameters in the array? Any specific type of syntax that might work?

Thanks again!
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: Expand string parameter list

17 Mar 2021, 20:04

You’d have to double-deref the string into a variable I believe. Using the asterisk to expand the array is quite common. Your function would need to be able to accept a varied number of parameters with defaults to account for a carried length array.
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Expand string parameter list

24 Mar 2021, 14:09

In my case, my functions are basic AHK scripting functions with specific parameters. The callers of the functions are written in HTML, so this code is acting as a translator. Most of the functions are specifically written to deal with HTML callers, but I was still curious to learn of any clever ways to allow the plain-text code to specify things on its own (such as requesting a double-deref).

I can always add my own syntax to make things happen, but it would work against the simplicity of this beautiful array expansion operator.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Albireo, sofista, wineguy and 38 guests