Variadic function call with named parameters ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Gotjek
Posts: 18
Joined: 11 Aug 2022, 11:01

Variadic function call with named parameters ?

Post by Gotjek » 23 Sep 2022, 07:38

Hi,

I search for this for some time but I can't find the answer.
I read the doc unsuccessfully (https://lexikos.github.io/v2/docs/Functions.htm#VariadicCall).

We can use named parameters when week-end call a function.
It is possible to store these named parameters in a variable, then pass this variable as parameter ?

Here is an example with pseudo-code :

Code: Select all

my_func(day, month, year) {
    MsgBox day " " month " " year
}

my_func(day := 12, month := 04, year := 2022)  ; This works.

params := {%day% : 12, %month% : 04, %year% : 2022}  ; Not sure about the "%"...
my_func(params*)  ; I would like this to work.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Variadic function call with named parameters ?  Topic is solved

Post by jNizM » 23 Sep 2022, 07:57

You could pass a map instead

Code: Select all

param := Map("day", 12, "month", 04, "year", 2022)

my_func(param)

my_func(params) {
	for key, value in params
		MsgBox key ": " value
}
and you are flexible with the keynames / values

Code: Select all

key1 := "day"
key2 := "month"
key3 := "year"
val1 := 12
val2 := 04
val3 := 2022

param := Map(key1, val1, key2, val2, key3, val3)

/*
param := Map()
param.Set(key1, val1, key2, val2, key3, val3)
*/

my_func(param)

my_func(params) {
	for key, value in params
		MsgBox key ": " value
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

iseahound
Posts: 1472
Joined: 13 Aug 2016, 21:04
Contact:

Re: Variadic function call with named parameters ?

Post by iseahound » 23 Sep 2022, 08:59

Support was removed, but functionality still remains on v1.

Code: Select all

fn({a: 2, b: 3}*)
fn(a, b) {
   MsgBox % a + b
}
You would have to ask @lexikos if he has any plans to re add named parameters.
Spoiler

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Variadic function call with named parameters ?

Post by safetycar » 23 Sep 2022, 10:09

iseahound wrote:
23 Sep 2022, 08:59

Code: Select all

fn({a: 2, b: 3}*)
fn(a, b) {
   MsgBox % a + b
}
I was going to check if it respects the order of parameters by changing "a" to "c" but only with that already breaks without getting the answer I wanted :lol: .
Edit: I was thinking it worked like arrays. But I rechecked modifying the parameter of the function too and it seems to use what it needs.
But the silent error of v1 was not helpful at all (anyway if it's not supported there's not much to expect).

Post Reply

Return to “Ask for Help (v2)”