lua-users home
lua-l archive

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


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