local metatable = {
__call = function(t, param)
local sum = {}
for i, value in ipairs(t) do
sum[i] = value + param -- Add the argument (5) to the value, then place it in the new table (t).
end
return unpack(sum) -- Return the individual table values
end
}
local t = setmetatable({10, 20, 30}, metatable)
print(t(5)) --> 15 25 35
This is one of those cases where ipairs/pairs/next is needed because the __call metamethod is invoked for any table that’s used for generalized iteration
Another way would be defining a __iter method for example:
local metatable = {
__call = function(t, param)
local sum = {}
for i, value in t do
sum[i] = value + param -- Add the argument (5) to the value, then place it in the new table (t).
end
return unpack(sum) -- Return the individual table values
end,
__iter = ipairs -- Could be a custom iterator function aswell
}
local t = setmetatable({10, 20, 30}, metatable)
print(t(5)) --> 15 25 35
Declaring the __iter metamethod and/or using pairs/ipairs is a valid way to bypass this error. However if you don’t want to modify the behavior of generalized iteration (ipairs iterates indexes until nil, pairs iterates indexes and then keys), rawget() is the right way to do this:
local t = setmetatable({}, {
__index = function(t, k)
return rawget(t, k)
end,
})