lua-users home
lua-l archive

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


I think you want the __index to be set on the mt, not the mmt.  For
example, this works fine:

local mmt = { __newindex = function() print "here" end}
local mt = setmetatable({}, mmt)
mt.__index = mt
local t = setmetatable({}, mt)
t.abc = "abc"

Alternatively, you could copy over the __newindex function like this:

local mmt = { __newindex = function() print "here" end}
local mt  = setmetatable({}, mmt)
mt.__newindex = mmt.__newindex
local t   = setmetatable({}, mt)
t.abc = "abc"

Cheers,
-Brian

On Fri, Aug 5, 2011 at 7:30 PM, Xavier Wang <weasley.wx@gmail.com> wrote:
> hi all,
>
> We can get metamethod in a metatable:
>
> local mt = { __newindex = function() print "here" end}
> local t = setmetatable({}, mt)
> t.abc --> here
>
> but if here are no __newindex in mt, lua dont request the metatable of
> the metatable:
>
> local mmt = { __newindex = functoin() print "here" end}
> mmt.__index = mmt
> local mt = setmetatable({}, mmt)
> print(mt.__newindex) --> a function
> local t = setmetatable({}, mt)
> t.abc --> nothing
>
> it means that, you can not set a metatable to a metatable, to control
> its behave.
>
> Is that deliberate? Is there any reason to do that?
>
>



-- 
Brian Maher >> Glory to God <<