[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: pairs() instead of ipairs() in Lua 5.2
- From: Ulrik Sverdrup <ulrik.sverdrup@...>
- Date: Sat, 22 May 2010 17:13:36 +0200
It follows a suggestion fitting for a minimalist language, but maybe
not a very pure solution.
Take the behavior of ipairs(..) in Lua 5.1 and make it a special case
of pairs() in Lua 5.2. What is needed is simply that the language
guarantees that the iterator returned by pairs() will return the
values corresponding to the keys 1 and upwards in order. The order
before that, or the order of any keys after the first nil-valued
integer keys is arbitrary.
This would mean that array-like tables (without anything other than
consecutive integer keys starting from 1) pairs() in Lua 5.2 would
behave like ipairs() does in Lua 5.1
* Only one function, pairs, and only one metamethod, __pairs, makes
the language simpler
* If you want to exclude the non-integer keys, use a numeric for or
customize the object with the __pairs metamethod.
Porting old ipairs() calls may still require writing a custom ipairs,
just like the suggestion in Lua 5.2 work3 basically does. The
difference is that future code is cleaner and more consistent -- We
will hopefully see more objects customized by using __pairs metamethod
for consistent iteration. The user won't be confused about whether
he/she should be using ipairs() or pairs() for custom objects, if this
suggestion is implemented.
(This suggestion followed naturally from a suggestion on the mailing
list to use replace ipairs with pairs, but Lua doesn't guarantee that
this will work, right now.)
Ulrik