lua-users home
lua-l archive

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



On Dec 10, 2014, at 12:19 PM, Andrew Starks <andrew.starks@trms.com> wrote:

On Wed, Dec 10, 2014 at 1:55 PM, Tim Hill <drtimhill@gmail.com> wrote:
As has been noted before, this does not have the same behavior as ipairs()
if the table is not a valid sequence. (Earlier betas of 5.3 did have this
behavior, but it has been reverted to stop the iteration at the first nil
value, which is not necessarily at #t+1 for non-sequences.)

I'm not quite following you.

Are you saying that if __len is not defined, 5.3 will always stop at
the first `nil` value, regardless of `#t`'s value? And to read into
that, if __len *is* defined, it will respect the length?

I'm still on an alpha and I think that my version has the now reverted
behavior, am I correct

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

For comparison, the 5.3 alpha (earlier) docs read:

"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]), ..., (#t,t[#t]).

The table should be a proper sequence or have a __len metamethod (see §3.4.7).”

In fact, if you believe the docs (as i think people should), ipairs() int 5.3 beta (and presumably the release as well unless it changes again), __len is not involved at all, and can return whatever it likes with no effect on ipairs().

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.

—Tim