lua-users home
lua-l archive

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

On 4/19/07, Jose Luis Hidalgo <> wrote:
I'm not sure if is there any reason, but maybe this can help you, it
is a replacement of pairs written in pure lua.

        local _pairs = pairs
        function pairs (value)
                if type(value) == "userdata" then
                        local g = getmetatable(value)
                        return g.__pairs(value)
                        return _pairs(value) -- original

Actually there's no reason to limit it to userdata. Whatever datum with a __pairs metamethod should let you override the behavior:

local mt = getmetatble(value)
return mt and mt.__pairs and mt.__pairs(value) or _pairs(value)

(Notice that the overhead happens outside of the for loop which typically uses pairs)
As for # not being overridable on tables, I never realized it, but that's quite puzzling. The worst part is, I don't think this can be fixed without touching the VM (except through a metalua macro of course :))