Type Function Library (globals) Return value Table Revision Release 2022.3683 Keywords table, metatable See also getmetatable()
Sets the metatable for the given table (you cannot change the metatable of other types from Lua, only from C). If metatable
is nil
, removes the metatable of the given table. If the original metatable has a __metatable
field, it raises an error.
This function returns the same table that was provided as the first argument of the function, now with its metatable set.
setmetatable( table, metatable )
Table. The Lua table whose metatable you want to modify.
Table. The Lua table to set as the new metatable for table
.
local t = {} local mt = { __index = t } function t.new() return setmetatable( {}, mt ) end return t