lua-users home
lua-l archive

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


Am 31.03.2014 19:29 schröbte Javier Guerra Giraldez:
On Mon, Mar 31, 2014 at 12:14 PM, Petite Abeille
<petite.abeille@gmail.com> wrote:
local aFunction = function() end
debug.setmetatable( aFunction, { __tostring = function() return 'm e t a  f u n c t i o n' end } )
print( aFunction )


....
local anotherFunction = function() .... end
print (anotherFunction)


so, you can't set metatable to _a_ function, you set the global
function type's metatable


You could do

    local func_mt = setmetatable( {}, { __mode = "k" } )

    debug.setmetatable( function() end, {
      __tostring = function( f )
        return (func_mt[f] and func_mt[f].__tostring or rawtostring)( f )
      end,
      -- other metamethods ...
    } )

    local aFunction = function() end
func_mt[ aFunction ] = { __tostring = function() return 'meta function' end } )

    print( aFunction )
    local anotherFunction = function() end
    print( anotherFunction )

Sadly we don't have `rawtostring` available in Lua, so this specific example is difficult, but in principle it should work ...

But I still have no idea what the OP really wants to do ...

Philipp