[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Getting a reference to the current function
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Wed, 4 Oct 2006 14:42:56 -0400
I think you can do something like that:
---------------------------------------
function transform(f)
local env = getfenv(f)
setfenv(f, setmetatable({callee=f}, { __index = env }))
return f
end
local display = transform( function(arg)
print(tostring(callee)..": "..tostring(arg))
end)
print("'display' is "..tostring(display))
display(32)
---------------------------------------
Another alternative is to use tables with a __call metamethod:
---------------------------------------
function transform(f)
local functable = {}
local env = getfenv(f)
setfenv(f, setmetatable({callee=functable}, { __index = env }))
setmetatable(functable, {
__call = function(t, ...) f(...) end;
})
return functable
end
local display = transform(function(arg)
print(tostring(callee.name)..": "..tostring(arg))
end)
display.name = "logger"
display(32)
---------------------------------------