Log In  


If unpack is used to populate a new table, and the unpack is followed by even more items, then only the first item from the unpack gets retained.

function print_tbl(t)
 ?'count:'..#t
 for k,v in pairs(t) do
  ?k..'='..v
 end
end

t0={
 a='a',b='b',
 unpack(split"1,2,3,4")
}
print_tbl(t0) -- this works as expected

t1={
 unpack(split"1,2,3,4"),
 a='a',b='b'
}
print_tbl(t1) -- 2,3,4 stomped out

t2={
 unpack(split"1,2,3,4"),
 unpack(split"5,6")
}
print_tbl(t2) -- 2,3,4 stomped out


1

Unfortunately, that's just how Lua works. The implicit tuple of a '...' argument, or the return values from a function, are only appended in full if it's the last one in a new tuple, e.g. an arglist, an assignment list, or a table list. Otherwise you only get the first value.


Bummer. Thanks for clarifying!



[Please log in to post a comment]