lua-users home
lua-l archive

Re: Check for ipairs() compat mode

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


Quoting tobias@justdreams.de:
Hi,
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
end
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 )
 idx=idx+1;
 if tbl[idx] then
 return idx, tbl[idx], 'foo'
 end end, tbl, 0
 end
 }
)
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?
Thanks,
--tobbik
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)

AltStyle によって変換されたページ (->オリジナル) /