- 標(biāo)題:
模擬元組 / 返回類元組閉包 - 標(biāo)簽:
AutoHotkey | AHK | 元組 | Tuple | 閉包 | Closure | 裝飾器 | Decorator | 批量返回 - 標(biāo)注:
http://www.itdecent.cn/p/3d8fa7480e86
http://www.itdecent.cn/u/1275d25b625e
在編寫腳本時(shí),我們有時(shí)希望以便捷的方式返回多個(gè)結(jié)果。
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類型,并含有一個(gè)靜態(tài)默認(rèn)調(diào)用,接受可變參數(shù)。
你可以這樣調(diào)用它:
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)
函數(shù)MyFunc接受兩個(gè)參數(shù),并返回Tuple類型;變量temp從函數(shù)中得到閉包,并在次行為res_a和res_b賦予解包結(jié)果。MsgBox打印的內(nèi)容為4 7。
至此,已可簡(jiǎn)略地在腳本中使用元組。