diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-09-17 16:07:48 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-09-17 16:07:48 -0300 |
| commit | 8fb1af0e33cd8688f57cd0e3ab86420a8cfe99bd (patch) | |
| tree | 2c199de5e67f395a4f9c94fcfb2ef3f0dad8882a /testes | |
| parent | 140b672e2ee2ac842661ece4b48e1a64f0cd11ea (diff) | |
| download | lua-8fb1af0e33cd8688f57cd0e3ab86420a8cfe99bd.tar.gz lua-8fb1af0e33cd8688f57cd0e3ab86420a8cfe99bd.tar.bz2 lua-8fb1af0e33cd8688f57cd0e3ab86420a8cfe99bd.zip | |
Varag parameter is a new kind of variable
To allow some optimizations on its use.
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/vararg.lua | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua index 4320684e..92f720cb 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua | |||
| @@ -150,5 +150,31 @@ do | |||
| 150 | local a, b = g() | 150 | local a, b = g() |
| 151 | assert(a == nil and b == 2) | 151 | assert(a == nil and b == 2) |
| 152 | end | 152 | end |
| 153 | |||
| 154 | |||
| 155 | do -- vararg parameter used in nested functions | ||
| 156 | local function foo (... = tab1) | ||
| 157 | return function (... = tab2) | ||
| 158 | return {tab1, tab2} | ||
| 159 | end | ||
| 160 | end | ||
| 161 | local f = foo(10, 20, 30) | ||
| 162 | local t = f("a", "b") | ||
| 163 | assert(t[1].n == 3 and t[1][1] == 10) | ||
| 164 | assert(t[2].n == 2 and t[2][1] == "a") | ||
| 165 | end | ||
| 166 | |||
| 167 | do -- vararg parameter is read-only | ||
| 168 | local st, msg = load("return function (... = t) t = 10 end") | ||
| 169 | assert(string.find(msg, "const variable 't'")) | ||
| 170 | |||
| 171 | local st, msg = load[[ | ||
| 172 | local function foo (... = extra) | ||
| 173 | return function (...) extra = nil end | ||
| 174 | end | ||
| 175 | ]] | ||
| 176 | assert(string.find(msg, "const variable 'extra'")) | ||
| 177 | end | ||
| 178 | |||
| 153 | print('OK') | 179 | print('OK') |
| 154 | 180 | ||
