lua-users home
lua-l archive

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



On Fri, Dec 5, 2008 at 10:57 PM, Jerome Vuarand <jerome.vuarand@gmail.com> wrote:
2008/12/5 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>> But how could I get the metatable for the prototype of all functions?
>
> The backdoor way is to use debug.getmetatable.

You can also wrap your function in a full userdata and override __call
and then __add (but I think __concat may be more suited to the task).
 
Thanks for the points.
For the moment, debug.[get/set]metatable is exactly what I need.
Just in case some other newbie arrives at this, here's how it works:

> function compose(f,g)
>>      return function(x)
>>              return f(g(x))
>>      end
>> end
> return debug.getmetatable(print)
nil
> mt = {}
> mt.__add = compose
> debug.setmetatable(print,mt)
> return debug.getmetatable(print)
table: 002FCC88
> function quote(x)
>>      return '"'..tostring(x)..'"'
>> end
> return debug.getmetatable(quote)
table: 002FCC88
> return quote(1)
"1"
> qqq = quote + quote + quote
> return qqq(1)
"""1"""