lua-users home
lua-l archive

Re: Function as metatable (metafunctions?)

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


On 31/03/2014 14:28, Coda Highland wrote:
On Mon, Mar 31, 2014 at 10:14 AM, Petite Abeille
<petite.abeille@gmail.com> wrote:
On Mar 31, 2014, at 2:57 AM, Thiago L. <fakedme@gmail.com> wrote:
I mean I can't set __metatable to a function so…
Eh?
local aFunction = function() end
debug.setmetatable( aFunction, { __tostring = function() return 'm e t a f u n c t i o n' end } )
print( aFunction )
That's setting the metatable OF a function.
What doesn't work is:
debug.setmetatable( aFunction, function(key) return --[[magic]] end )
I myself am still trying to figure out the exact USE case, but I at
least know what's being asked for.
/s/ Adam
local stringmetatable = getmetatable("")
debug.setmetatable("", function(key)
if key == "__metatable" then
-- some magic
else
-- some other magic to get the env and do stuff (and then we use the stuff from the __metatable hack)
end)
Or something like that... (and then you can just do getmetatable("").__index = whatever instead of getmetatable("")().__index = whatever, so it's seamless sandboxing)

AltStyle によって変換されたページ (->オリジナル) /