lua-users home
lua-l archive

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


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.