Lua Nested Unpack Bug?

Question:

I am trying to unpack an array into an array, but it only works if it was unpacked last, if there is something after it, only the first element is unpacked. Below is a very simple example of what I'm trying to do. Is there a better way to do this, or is this a mistake I will have to deal with? I do not want to use table.insert, as this seems much more readable by adding something like unpack to the table definition.

the code:

   print ("Error 1")
   local table1 = { {1,1}, {2,2}, {3,3} }
   local table2 = { {0,0}, unpack (table1), {4,4} }
   for n,item in ipairs (table2) do print (unpack(item)) end

   print ("Good")
   table1 = { {1,1}, {2,2}, {3,3} }
   table2 = { {0,0}, unpack (table1) }
   for n,item in ipairs (table2) do print (unpack(item)) end

   print ("Error 2")
   table1 = { {1,1}, {2,2}, {3,3} }
   table2 = { {0,0}, unpack (table1), unpack (table1) }
   for n,item in ipairs (table2) do print (unpack(item)) end

Conclusion:

Error 1
0       0
1       1 -- {2,2} & {3,3} cut off.
4       4
Good
0       0
1       1 -- All elements unpacked.
2       2
3       3
Error 2
0       0
1       1 -- {2,2} & {3,3} cut off.
1       1 -- All elements unpacked.
2       2
3       3

Note:

I am running version 5.1.

+3
source share
1 answer

It's not a mistake. A function call that returns multiple values ​​is configured to the first value if the call is not the last. The manual says that at http://www.lua.org/manual/5.1/manual.html#2.5

+11
source

Source: https://habr.com/ru/post/1794607/


All Articles