lua-users home
lua-l archive

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


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)
---------------------------------------