lua-users home
lua-l archive

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



Lua 5.3 had an ipairs() Compat which seemed enabled by default. 5.4 has done away with it.

I have a implementation of an ordered hash table where I can

for i,v,k in ipairs(o) do
   print(i,v,k) -- k is the key for value at index i

The newer implementation of ipairs() does not honour if __ipairs returns more than 2 values. Not a big issue but it would be nice if we can test for it. Here is my (rather
verbose) implementation:

t = setmetatable( {0,1,2,3,4,5,6,7,8,9},
      __ipairs  = function( tbl )
         local idx = 0
         return function( key )
            if tbl[idx] then
               return idx, tbl[idx], 'foo'
            end end, tbl, 0

a,b,c = ipairs(t)
x,y,z = a(b,c)

if 'foo' == z then IPAIRS_IS_COMPAT end

Is there an easier way to test for it?


I guess I can shorten it to the minimum but it's still clumsy:

local t = setmetatable( {},{__ipairs=function(tbl) return function() return nil,nil,true end,tbl,0 end})

local a,b,c = ipairs(t)
local _,_,_isCompat = a(b,c)