lua-users home
lua-l archive

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


Petite Abeille <petite.abeille <at> gmail.com> writes:

> 
> 
> On Jun 27, 2008, at 8:56 PM, Tracy Hill wrote:
> 
> > Is there a way to use the metatable __add concept on numbers( and  
> > booleans)?
> 
> You can try debug.setmetatable (object, table):
> 
> http://www.lua.org/manual/5.1/manual.html#pdf-debug.setmetatable
> 
> Beware of unintended consequences as this might be too clever by half :)
> 
> --
> PA.
> http://alt.textdrive.com/nanoki/
> 
> 


It looks like debug.setmetatable is what I'm looking for.

I'm not quite sure how to use it.  The following code snippet didn't work for me.

Any suggestions?


    debug.setmetatable(0, {
        __add = function(o1, o2)
            print("custom adding: " .. o1 .. " " .. o1)
            return 47 -- everything is 47
        end
    })

    print("This should be 47: " .. 1 + 2) -- its 3 not 47