可变长函数
Lua中的可变长函数的参数用 ... 来表示 ( 3个 . )
在函数内部有一个特殊的 内置变量 arg
其格式如下
arg =
{
1,
"Hello",
true,
n = 3
}
-- function makeVarStr(...) to see print_Table.lua
function printMultiArg(...)
print("... = " .. makeVarStr(arg))
end
function my_print1(fmt, ... )
print("=================== Begin my_print1 ===================")
print("fmt = " .. makeVarStr(fmt))
print("... = " .. makeVarStr(arg))
print("=================== End my_print1 ===================\n")
end
function my_print2(fmt, ... )
print("=================== Begin my_print2 ===================")
print("fmt = " .. makeVarStr(fmt))
printMultiArg(arg)
print("=================== End my_print2 ===================\n")
end
function my_print3(fmt, ... )
print("=================== Begin my_print3 ===================")
print("fmt = " .. makeVarStr(fmt))
printMultiArg(...)
print("=================== End my_print3 ===================\n")
end
a =1
b = "Hello"
c = true
my_print1("a = ??? , b = ??? , c= ??? ",a,b,c)
my_print2("a = ??? , b = ??? , c= ??? ",a,b,c)
my_print3("a = ??? , b = ??? , c= ??? ",a,b,c)
--[[
=================== Begin my_print1 ===================
fmt = "a = ??? , b = ??? , c= ??? "
... =
{
[1] = 1,
[2] = "Hello",
[3] = true,
"n" = 3,
}
=================== End my_print1 ===================
=================== Begin my_print2 ===================
fmt = "a = ??? , b = ??? , c= ??? "
... =
{
[1] =
{
[1] = 1,
[2] = "Hello",
[3] = true,
"n" = 3,
},
"n" = 1,
}
=================== End my_print2 ===================
=================== Begin my_print3 ===================
fmt = "a = ??? , b = ??? , c= ??? "
... =
{
[1] = 1,
[2] = "Hello",
[3] = true,
"n" = 3,
}
=================== End my_print3 ===================
]]
##################################################
Nice Version 1
Use select(n, ...) expression
##################################################
--[[
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Lua Interpret will assignment
// The command line arguments as the internal build-in argument named "arg"
// instead of "..." in the function argument list
//
// Variadic Arguments
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////
--]]
function printAll(fmt, ...)
print("---------- Begin printAll() ----------\n")
print("fmt = " .. fmt)
print("arg = " , ... )
print()
--
-- yield the argument list's Count
--
print("# ... = ", select("#", ...) )
print("******************************")
for i=1,select("#", ...) do
-- Here : ( ) is a must
-- ********************************************************************************************************************
-- ( ) ==> force "select(...)" function return only one argument rathan than multiple arguments
-- ********************************************************************************************************************
print(" ",i," = ", (select(i, ...)) )
end
print("******************************\n")
print("---------- End printAll() ----------")
end
printAll("Hello-World : ", "a1", "b2", "c3" , 3.14, true, false, nil , "end");
---------- Begin printAll() ----------
fmt = Hello-World :
arg = a1 b2 c3 3.14 true false nil end
# ... = 8
1 = a1
2 = b2
3 = c3
4 = 3.14
5 = true
6 = false
7 = nil
8 = end
---------- End printAll() ----------
##################################################################
Nice Version 2
Use 'arg' internal varible
instead of '...'* <= Variadic arguments
#################################################################
print("--------------------------------------------------")
print("outside arg = ",arg)
print("outside arg.n = ",arg.n)
print("--------------------------------------------------\n")
local newgt = {
printAll = function(fmt, ...)
print("fmt = ",fmt)
print("inner arg = ", arg)
print("inner arg.n = ", arg.n)
for i,v in pairs(arg) do
print("\t",i,v)
end
return "Done"
end
}
setmetatable(newgt, { __index = _G })
-- setfenv(...) is avaliable only in lua5.1
setfenv(1, newgt)
print(" [1] [2] [3] [4] [5] [6] [7]")
print("Calling " .. [[printAll("format : %d%c", "a1", "b2", "c3", 3.14, true, nil ,false)]])
print( printAll("format : %d%c", "a1", "b2", "c3", 3.14, true, nil ,false) )
outside arg = table: 00B302A0
outside arg.n = nil
[1] [2] [3] [4] [5] [6] [7]
Calling printAll("format : %d%c", "a1", "b2", "c3", 3.14, true, nil ,false)
fmt = format : %d%c
inner arg = table: 00B30138
inner arg.n = 7
1 a1
2 b2
3 c3
4 3.14
5 true
7 false
n 7
Done