spread an array in function parameters? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hpta
Posts: 24
Joined: 27 Sep 2019, 01:43

spread an array in function parameters?

26 Jul 2021, 22:12

Is it possible in the following example to do something like? (preferably in a simple way)

Code: Select all

this.f.Call(this.params.spread())
instead of

Code: Select all

this.f.Call(this.params[1], this.params[2], this.params[3])

Code: Select all

class F_obj {
   f := ""
   params := ""
   __New(f, params*) {
      this.f := f
      this.params := params
   }
   Call() {
   ; this works but it's cumbersome
      ; this.f.Call(this.params[1], this.params[2], this.params[3])
      
   ; this doesn't work
       this.f.Call(this.params)
   }
}

F1 := F_obj(foo, "a", "b", "c")
F1()

foo(p1,p2,p3) {
   msgbox p1 p2 p3
}
hpta
Posts: 24
Joined: 27 Sep 2019, 01:43

Re: spread an array in function parameters?

27 Jul 2021, 05:19

Thanks! I really got stuck on this.
User avatar
Cornelius-Figgle
Posts: 5
Joined: 20 Mar 2023, 13:44
Contact:

Re: spread an array in function parameters?

03 Apr 2023, 05:24

swagfag wrote:
26 Jul 2021, 22:31

Code: Select all

this.f.Call(this.params*)
I know this is an old topic, but is this possible with a map?

In Python you can have to following:

Code: Select all

function_with_positional_args(*list_of_positional_args)
function_with_keyword_args(**dict_of_keyword_args)
Where * expands a list (ie an array) and ** expands a dictionary (ie a map) - the dictionary keys have to match the parameter names

Using 2 asterisks on a map in AHK throws an error, and using one seems to pass the key names through rather than the values. Is there anyway to pass the map values to function parameters (so that the keys match the parameter names)?
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: spread an array in function parameters?

05 Apr 2023, 14:17

Cornelius-Figgle wrote:
03 Apr 2023, 05:24
Is there anyway to pass the map values to function parameters (so that the keys match the parameter names)?
@Cornelius-Figgle, I don't believe there is a built-in way to do so. You could achieve that by defining a Values property for the Map's in your script as follows:

Code: Select all

DefineMapValuesProperty()
M := Map('a', 'aa', 'b', 'bb', 'c', 'cc')
MsgBox List(M.Values*)

DefineMapValuesProperty() {
   Map.Prototype.DefineProp('Values', {Get: GetValues})
   GetValues(this) {
      Values := []
      for , Value in this
         Values.Push(Value)
      return Values
   }
}

List(Params*) {
   List := ''
   for Value in Params
      List .= Value '`n'
   return List
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: akirofe, erann, kiwichick and 61 guests