19 окт. 2008 г.

Protected metatables

If you set a __metatable field in the metatable, getmetatable will return the value of this field, whereas setmetatable will raise an error.

Roberto Ierusalimschy, Programming in Lua 1st edition, 13.3 - Library-Defined Metamethods

> mt = { __metatable = true }
> t = {}
> setmetatable(t, mt)
> setmetatable(t, mt)
stdin:1: cannot change a protected metatable
stack traceback:
[C]: in function 'setmetatable'
stdin:1: in main chunk
[C]: ?

Комментариев нет: