lua-users home
lua-l archive

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


On Mar 24, 2009, at 6:00 PM, Luiz Henrique de Figueiredo wrote:

Is there any reason not to support metatables for functions?

There is support for metatables for function, only not from Lua, just from C.
Or you can use debug.setmetatable. See the example below.

debug.setmetatable(print,{
__add = function(f,g) return function (...) return g(f(...)) end end})

function f(x) return x+1 end
function g(x) return x^2 end

h=f+g assert(h(2)==9)
h=g+f assert(h(2)==5)

Yes, but that applies the same metatable to all functions. I should perhaps have been more precise and said "per function metatables" (defaulting back to a shared metatable for compatibility).

Mark