- 标题:
模拟元组 / 返回类元组闭包 - 标签:
AutoHotkey | AHK | 元组 | Tuple | 闭包 | Closure | 装饰器 | Decorator | 批量返回 - 标注:
https://www.jianshu.com/p/3d8fa7480e86
https://www.jianshu.com/u/1275d25b625e
在编写脚本时,我们有时希望以便捷的方式返回多个结果。
class Tuple {
static Call(T*) {
input := Array(T*)
unpacker(&vars*) {
if (vars.Length != input.Length)
throw "Tuple unpacking error: " input.Length " elements expected, but " vars.Length " variables provided."
for var in vars
%var% := input[A_Index]
}
return unpacker
}
}
上述代码定义了Tuple
类型,并含有一个静态默认调用,接受可变参数。
你可以这样调用它:
MyFunc(a, b) {
return Tuple(a+1, b+1)
}
temp := MyFunc(3, 6)
temp(&res_a, &res_b)
;MyFunc(3, 6)(&res_a, &res_b) ; 也可以这样。
MsgBox(res_a " " res_b)
函数MyFunc
接受两个参数,并返回Tuple
类型;变量temp
从函数中得到闭包,并在次行为res_a
和res_b
赋予解包结果。MsgBox
打印的内容为4 7
。
至此,已可简略地在脚本中使用元组。