[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Functions with individual metatables
- From: Richard Hundt <richardhundt@...>
- Date: Tue, 19 May 2009 10:01:22 +0200
steve donovan wrote:
[...]
fmeta = {}
fmeta.__index = fmeta
fmeta.attrb = 'hello'
debug.setmetatable(math.sin,fmeta)
return math.sin.attrb
hello
return math.cos.attrb
hello
They all have the same attribute, which is probably not useful. Hence
the need for individual metatables.
No indeed, it doesn't do boxing. I stopped thinking at the "tag a
function" part :)
To make your test work, we need to delegate:
local func_meta = { }
setmetatable(func_meta, { __mode = "k" })
debug.setmetatable(function() end, {
__index = function(func, key)
local meta = func_meta[func]
if meta and meta.__index then
if type(meta.__index) == "function" then
return meta:__index(key)
else
return meta.__index[key]
end
end
end;
__newindex = function(func, key, val)
local meta = func_meta[func]
if meta and meta.__newindex then
if type(meta.__newindex) == "function" then
return meta:__newindex(key, val)
else
return meta.__newindex[key]
end
end
end;
-- the rest of the meta table events here...
})
function setfmeta(func, meta)
func_meta[func] = meta
return func
end
function getfmeta(func)
return func_meta[func]
end
fmeta = {}
fmeta.__index = fmeta
fmeta.attrb = 'hello'
setfmeta(math.sin, fmeta)
print(math.sin.attrb)
This all is starting to look significantly more expensive, so I'll have
to have a little think about how to optimise it.
Cheers,
Rich