定义函数参数的默认值 [英] Define default values for function arguments
问题描述
function myfunction(a,b, c)
b = b或7
c = c或5
print(a,b,c)
结束
这是唯一的方法吗? PHP风格 myfunction(a,b = 7,c = 5)
似乎不起作用。不是说Lua的方式不行,我只是想知道这是否是唯一的方法。
如果你需要命名参数和默认值如PHP或Python,你可以用一个表格构造函数调用你的函数:
myfunction {a, b = 3,c = 2}
(这在Lua的很多地方都可以看到,一个href =http://w3.impa.br/~diego/software/luasocket/http.html =noreferrer> LuaSocket协议模块的高级形式和构造函数 IUPLua 。)
函数本身可以有这样的签名:
pre $ function myfunction(t)
setmetatable(t,{__ index = {b = 7, c = 5}})
本地a,b,c =
t [1]或ta,
t [2]或tb,
t [3]或tc
- 函数在这里继续...
end
缺少的任何值参数表将从其元表(参见 __ index 表中获取。 html#2.8rel =noreferrer>有关metatables的文档)。
当然,更高级的参数样式可以使用表构造函数和函数 - 你可以写任何你需要的东西。例如, here 是一个函数,它构造一个函数,该函数从表中获取名称或位置参数表定义参数名称和默认值以及采用常规参数列表的函数。
作为非语言级别的功能,可以更改此类调用以提供新行为并语义:
- 变量可以接受多个名称
- 位置变量和关键字变量可以散布 - 定义两者都可以优先于(或导致错误)
- 可以创建关键字无位置变量,以及无名位置专用变量
- 通过解析一个字符串可以完成相当详细的表结构
- 如果函数被调用的时间不是1个表,则可以逐字使用参数列表
一些用于写入参数tr的有用函数答案是 unpack
(在5.2中移动到 table.unpack
), setfenv
(在5.2版中不推荐使用新的 _ENV
构造),并且选择
给定的参数列表,或列表的长度'#'
)。
In the Lua wiki I found a way to define default values for missing arguments:
function myfunction(a,b,c)
b = b or 7
c = c or 5
print (a,b,c)
end
Is that the only way? The PHP style myfunction (a,b=7,c=5)
does not seem to work. Not that the Lua way doesn't work, I am just wondering if this is the only way to do it.
If you want named arguments and default values like PHP or Python, you can call your function with a table constructor:
myfunction{a,b=3,c=2}
(This is seen in many places in Lua, such as the advanced forms of LuaSocket's protocol modules and constructors in IUPLua.)
The function itself could have a signature like this:
function myfunction(t)
setmetatable(t,{__index={b=7, c=5}})
local a, b, c =
t[1] or t.a,
t[2] or t.b,
t[3] or t.c
-- function continues down here...
end
Any values missing from the table of parameters will be taken from the __index
table in its metatable (see the documentation on metatables).
Of course, more advanced parameter styles are possible using table constructors and functions- you can write whatever you need. For example, here is a function that constructs a function that takes named-or-positional argument tables from a table defining the parameter names and default values and a function taking a regular argument list.
As a non-language-level feature, such calls can be changed to provide new behaviors and semantics:
- Variables could be made to accept more than one name
- Positional variables and keyword variables can be interspersed - and defining both can give precedence to either (or cause an error)
- Keyword-only positionless variables can be made, as well as nameless position-only ones
- The fairly-verbose table construction could be done by parsing a string
- The argument list could be used verbatim if the function is called with something other than 1 table
Some useful functions for writing argument translators are unpack
(moving to table.unpack
in 5.2), setfenv
(deprecated in 5.2 with the new _ENV
construction), and select
(which returns a single value from a given argument list, or the length of the list with '#'
).
这篇关于定义函数参数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!