lua-users home
lua-l archive

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

2018-07-19 1:35 GMT+02:00  <>:

> 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.

There is no such thing as "the newer implementation of ipairs()".

The 5.4 manual says exactly what the 5.3 manual said, that ipairs
"Returns three values (an iterator function, the table t, and 0)",

It no longer says that "The ipairs iterator now respects metamethods
and its __ipairs metamethod has been deprecated." and in fact the
__ipairs metamethod still works as in 5.3, with 'ipairs' adjusting the
number of return values from __ipairs to exactly three, without
checking them in any way.

$ lua5.4
Lua 5.4.0  Copyright (C) 1994-2017, PUC-Rio
> t = setmetatable({},{__ipairs=function(tbl) return 'the','quick','brown','fox' end})
> ipairs(t)
the    quick    brown

What may be true (since you have not given an actual example,
I can't check this) is that the function returned by the built-in ipairs
behaves differently from the one in Lua 5.3, but if you are supplying
your own __ipairs, this should not matter.