[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why can not using metatable to get meta-method?
- From: Brian Maher <brian@...>
- Date: Fri, 5 Aug 2011 20:57:36 -0700
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 <<