lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

section "Changes in the Libraries" has this:

  The Table library now respects metamethods for setting
  and getting elements.


a) Considering the place of this sentence, where will this information
   go in the Lua 5.4 manual?  Will it just go away (as long as Lua 5.4
   doesn't introduce a new incompatibility to that behaviour)?  I think
   this information is worth a sentence in one of the preceding
   sections.

b) In the current form, the above mentioned sentence is vague.  What is
   setting or getting an element?  Does table.insert count as "setting
   an element"?  Does table.concat count as "getting an element"?  What
   about table.unpack?  A test shows that none of these functions, that
   I'd consider part of "the table library", respect a table's __index
   metamethod.

-- A table with three entries.
local t = { 1, 2, 3 }
-- We add a fourth one that is accessible via
-- the __index metamethod.
local mt = {
  __index = { [4] = 4 },
}
setmetatable(t, mt)
-- How many elements can we iterate over?
local i = 1
while t[i] ~= nil do-- 1 ... 4
  print(i, t[i])
  i = i + 1
end
print('table.concat: ', table.concat(t))-- Where's the four?
print('table.unpack: ', table.unpack(t))-- Where's the four?
-- OK, what about table.insert?
print('inserting element "x"')
table.insert(t, 'x')-- Where does the four go?
-- How many elements can we iterate over?
local i = 1
while t[i] ~= nil do-- 1 ... 4, again
  print(i, t[i])
  i = i + 1
end

What metamethods do each of the functions in the table library respect?

Best regards,
Stephan Hennig