Lua
-- 面向对象的写法
local Vector = {}
function Vector.new(x, y)
return setmetatable({ x = x or 0, y = y or 0 }, Vector)
end
setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })
local vector = Vector(1, 2)
-- or, 两者等效
local vector = Vector.new(1, 2)
-- Variable Arguments, Functions support the capturing of a varying number of arguments:
function sum(...)
local ret = 0
for i, v in ipairs{...} do ret = ret + v end
return ret
end
sum(3, 4, 5, 6) -- 18
-- To do so, just put `…` at the end of your argument list.
-- You can access members of the list of arguments by putting them
-- in a `table ({...})` or through the select function:
function sum(...)
local ret = 0
for i = 1, select("#", ...) do ret = ret + select(i, ...) end
return ret
end
-- `...` 不代表任何数据类型,只是表示这里有一排数据
-- lua string.match 中参于特殊字符的转义使用的是 `%`, 而不是 `\`
print(string.match("hello.world", "(.*)%.(.*)")) -- hello world
-- 如果使用的是 ngx.match 判断的话,需要用 \\ 转义
ngx.match(path, "[[\\n\\r\\t]") -- 捕获 \r \n \t 字符