There no __getn
metamethod. Try __len
instead. This works only on Lua 5.2
You cannot overload the #
operator for tables in Lua 5.1
You could use userdata to create a proxy object:
t = newproxy(true)
getmetatable(t).__len = function()
return 5
end
print(#t) --> 5
Note however, that the newproxy
function is undocumented.