[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Unexpected calculation result with Lua53
- From: Jonathan Goble <jcgoble3@...>
- Date: Sun, 3 May 2015 01:29:58 -0400
On Sun, May 3, 2015 at 1:12 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> local default_type = type
> type = function(x)
> local T=default_type(x)
> if T=="number" then T=math.type(x) end
> return T
> end
According to the manual, math.type(x) returns nil if x is not a
number. So I think you could shorten that code to this:
local default_type = type
type = function (x)
return math.type(x) or default_type(x)
end
In addition, code that needs this behavior is probably going to call
type() on numbers far more often than any other type, so you gain
efficiency by calling the more common case first, and by not having to
create a local variable or perform a string equality test.