lua-users home
lua-l archive

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



On Mar 24, 2006, at 18:41, Paul Chiusano wrote:

Now I can do (7):factorial() and voila! returns 5040! And it seems
that I can pass any number I want into debug.setmetatable, with the
same effect. Pretty snazzy.

Very cool :)

More madness:

local number = {}

setmetatable( number, { __index = math } )

function number:toDate( aFormat )
        return os.date( aFormat or "!%a, %d %b %Y %H:%M:%S GMT", self )
end

function number:days()
        return self * 60 * 60 * 24
end

function number:ago()
        return os.time() - self
end

debug.setmetatable( 1, { __index = number } )

print( ( 5 ):days():ago():toDate() )

> Thu, 23 Mar 2006 06:41:12 GMT

My question is: is there a performance penalty associated with doing
this? Does adding this metatable cause all numeric operations to
perform slower?

I was wondering about this as well. Furthermore, why isn't the math package the default metatable for numbers in the same way as the string package is for strings?

There must be something very obvious, as after asking the same question several time, no one has bothered answering one way or another. Oh, well... let me shoot myself in the foot 8^)

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/