lua-users home
lua-l archive

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


2014-12-11 11:34 GMT+02:00 Tim Hill <drtimhill@gmail.com>:

> The 5.3 beta docs state, for ipairs():
>
> "Returns three values (an iterator function, the table t, and 0) so that the
> construction
>
>      for i,v in ipairs(t) do body
>  end
>
> will iterate over the pairs (1,t[1]), (2,t[2]), ..., up to the first integer
> key absent from the table."
>
> My reading of “absent from the table” is “that returns nil as its value”.
> Note that this makes no mention of the # operator or the __len() metamethod.
> Again, my reading of the docs is that ipairs() in the 5.3 beta doesn’t even
> use # or __len().

The 5.3-beta docs state in the "Changes" section that ipairs and
the table library respect metamethods without specifying which.

> I’ve not tested this, but if ipairs() DOES respect __len() in some way on
> 5.3 beta, then imho either the code or the docs are wrong.

The change in ipairs from 5.2 is that __index is respected in 5.3-beta.
If __index invokes __len, then __ipairs may depend on __len.

mt = {
__len = function(self) return 5 end;
__index = function(self,i) if i>#self and i<=6 then return 100 end end }
t=setmetatable({1,2,3,4},mt)
for k,v in ipairs(t) do print(k,v) end
mt.__len = nil
for k,v in ipairs(t) do print(k,v) end

The 5.2 docs did not state that ipairs access is raw, but did so
for the table library. Nobody complained then. :-)