Vertical bar removed from syntax of vararg table

The syntax 'function foo (a, b, ...arg)' is already used by JavaScript
for this same semantics, so it seems natural to use the same notation in
Lua.
This commit is contained in:
Roberto I
2025-10-30 11:07:01 -03:00
parent 0149b781d4
commit d342328e5b
5 changed files with 20 additions and 22 deletions

View File

@@ -1079,8 +1079,8 @@ static void parlist (LexState *ls) {
} }
case TK_DOTS: { case TK_DOTS: {
varargk |= PF_ISVARARG; varargk |= PF_ISVARARG;
luaX_next(ls); luaX_next(ls); /* skip '...' */
if (testnext(ls, '|')) { if (ls->t.token == TK_NAME) {
new_varkind(ls, str_checkname(ls), RDKVAVAR); new_varkind(ls, str_checkname(ls), RDKVAVAR);
varargk |= PF_VAVAR; varargk |= PF_VAVAR;
} }

View File

@@ -2354,8 +2354,7 @@ initialized with the argument values:
@Produc{ @Produc{
@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or
varargparam} varargparam}
@producname{varargparam}@producbody{@bnfter{...} @producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}}
@bnfopt{@bnfter{|} @bnfNter{Name}}}
} }
When a Lua function is called, When a Lua function is called,
it adjusts its list of @x{arguments} to it adjusts its list of @x{arguments} to
@@ -2396,7 +2395,7 @@ g(5, r()) a=5, b=1, ... -> 2 3
} }
The presence of a varag table in a variadic function is indicated The presence of a varag table in a variadic function is indicated
by the @T{|name} syntax after the three dots. by a name after the three dots.
When present, When present,
a vararg table behaves like a read-only local variable a vararg table behaves like a read-only local variable
with the given name that is initialized with a table. with the given name that is initialized with a table.
@@ -9773,8 +9772,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or
varargparam} varargparam}
@producname{varargparam}@producbody{@bnfter{...} @producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}}
@bnfopt{@bnfter{|} @bnfNter{Name}}}
@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} @producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}}

View File

@@ -310,7 +310,7 @@ do -- testing presence of second argument
local function foo (howtoclose, obj, n) local function foo (howtoclose, obj, n)
local ca -- copy of 'a' visible inside its close metamethod local ca -- copy of 'a' visible inside its close metamethod
do do
local a <close> = func2close(function (... | t) local a <close> = func2close(function (...t)
assert(select("#", ...) == n) assert(select("#", ...) == n)
assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj)) assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj))
ca = 15 -- final value to be returned if howtoclose=="scope" ca = 15 -- final value to be returned if howtoclose=="scope"
@@ -910,7 +910,7 @@ do
local extrares -- result from extra yield (if any) local extrares -- result from extra yield (if any)
local function check (body, extra, ...|t) local function check (body, extra, ...t)
local co = coroutine.wrap(body) local co = coroutine.wrap(body)
if extra then if extra then
extrares = co() -- runs until first (extra) yield extrares = co() -- runs until first (extra) yield

View File

@@ -126,13 +126,13 @@ testamem("coroutine creation", function()
end) end)
do -- vararg tables do -- vararg tables
local function pack (... | t) return t end local function pack (...t) return t end
local b = testamem("vararg table", function () local b = testamem("vararg table", function ()
return pack(10, 20, 30, 40, "hello") return pack(10, 20, 30, 40, "hello")
end) end)
assert(b.aloc == 3) -- new table uses three memory blocks assert(b.aloc == 3) -- new table uses three memory blocks
-- table optimized away -- table optimized away
local function sel (n, ...|arg) return arg[n] + arg.n end local function sel (n, ...arg) return arg[n] + arg.n end
local b = testamem("optimized vararg table", local b = testamem("optimized vararg table",
function () return sel(2.0, 20, 30) end) function () return sel(2.0, 20, 30) end)
assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case

View File

@@ -3,7 +3,7 @@
print('testing vararg') print('testing vararg')
local function f (a, ...|t) local function f (a, ...t)
local x = {n = select('#', ...), ...} local x = {n = select('#', ...), ...}
assert(x.n == t.n) assert(x.n == t.n)
for i = 1, x.n do for i = 1, x.n do
@@ -20,7 +20,7 @@ local function c12 (...)
return res, 2 return res, 2
end end
local function vararg (... | t) return t end local function vararg (... t) return t end
local call = function (f, args) return f(table.unpack(args, 1, args.n)) end local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
@@ -153,8 +153,8 @@ end
do -- vararg parameter used in nested functions do -- vararg parameter used in nested functions
local function foo (... | tab1) local function foo (...tab1)
return function (... | tab2) return function (...tab2)
return {tab1, tab2} return {tab1, tab2}
end end
end end
@@ -165,11 +165,11 @@ do -- vararg parameter used in nested functions
end end
do -- vararg parameter is read-only do -- vararg parameter is read-only
local st, msg = load("return function (... | t) t = 10 end") local st, msg = load("return function (... t) t = 10 end")
assert(string.find(msg, "const variable 't'")) assert(string.find(msg, "const variable 't'"))
local st, msg = load[[ local st, msg = load[[
local function foo (... | extra) local function foo (...extra)
return function (...) extra = nil end return function (...) extra = nil end
end end
]] ]]
@@ -179,19 +179,19 @@ end
do -- _ENV as vararg parameter do -- _ENV as vararg parameter
local st, msg = load[[ local st, msg = load[[
local function aux (... | _ENV) local function aux (... _ENV)
global <const> a global <const> a
a = 10 a = 10
end ]] end ]]
assert(string.find(msg, "const variable 'a'")) assert(string.find(msg, "const variable 'a'"))
local function aux (... | _ENV) local function aux (..._ENV)
global a; a = 10 global a; a = 10
return a return a
end end
assert(aux() == 10) assert(aux() == 10)
local function aux (... | _ENV) local function aux (... _ENV)
global a = 10 global a = 10
return a return a
end end
@@ -200,7 +200,7 @@ end
do -- access to vararg parameter do -- access to vararg parameter
local function notab (keys, t, ... | v) local function notab (keys, t, ...v)
for _, k in pairs(keys) do for _, k in pairs(keys) do
assert(t[k] == v[k]) assert(t[k] == v[k])
end end
@@ -216,7 +216,7 @@ do -- access to vararg parameter
assert(m == collectgarbage"count") assert(m == collectgarbage"count")
-- writing to the vararg table -- writing to the vararg table
local function foo (... | t) local function foo (...t)
t[1] = t[1] + 10 t[1] = t[1] + 10
return t[1] return t[1]
end end