diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-09-16 13:26:24 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-09-16 13:26:24 -0300 |
| commit | 140b672e2ee2ac842661ece4b48e1a64f0cd11ea (patch) | |
| tree | b925cd1e40712ab09a75ef2c0e30095aac0af0aa /testes | |
| parent | 9ea06e61f20ae34974226074fc6123dbb54a07c2 (diff) | |
| download | lua-140b672e2ee2ac842661ece4b48e1a64f0cd11ea.tar.gz lua-140b672e2ee2ac842661ece4b48e1a64f0cd11ea.tar.bz2 lua-140b672e2ee2ac842661ece4b48e1a64f0cd11ea.zip | |
Vararg table
Not yet optimized nor documented.
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/vararg.lua | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua index 10553de2..4320684e 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua | |||
| @@ -3,9 +3,12 @@ | |||
| 3 | 3 | ||
| 4 | print('testing vararg') | 4 | print('testing vararg') |
| 5 | 5 | ||
| 6 | local function f (a, ...) | 6 | local function f (a, ...=t) |
| 7 | local x = {n = select('#', ...), ...} | 7 | local x = {n = select('#', ...), ...} |
| 8 | for i = 1, x.n do assert(a[i] == x[i]) end | 8 | assert(x.n == t.n) |
| 9 | for i = 1, x.n do | ||
| 10 | assert(a[i] == x[i] and x[i] == t[i]) | ||
| 11 | end | ||
| 9 | return x.n | 12 | return x.n |
| 10 | end | 13 | end |
| 11 | 14 | ||
| @@ -17,7 +20,7 @@ local function c12 (...) | |||
| 17 | return res, 2 | 20 | return res, 2 |
| 18 | end | 21 | end |
| 19 | 22 | ||
| 20 | local function vararg (...) return {n = select('#', ...), ...} end | 23 | local function vararg (...=t) return t end |
| 21 | 24 | ||
| 22 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end | 25 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end |
| 23 | 26 | ||
| @@ -99,7 +102,7 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil) | |||
| 99 | 102 | ||
| 100 | 103 | ||
| 101 | -- varargs for main chunks | 104 | -- varargs for main chunks |
| 102 | local f = load[[ return {...} ]] | 105 | local f = assert(load[[ return {...} ]]) |
| 103 | local x = f(2,3) | 106 | local x = f(2,3) |
| 104 | assert(x[1] == 2 and x[2] == 3 and x[3] == undef) | 107 | assert(x[1] == 2 and x[2] == 3 and x[3] == undef) |
| 105 | 108 | ||
