lua-users home
lua-l archive

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


The trouble is that this would unnecessarily slow down pairs() for 100% of its existing use cases. As the manual says, if you really need this then you can write it.

On Sat, 22 May 2010 08:13:36 -0700, Ulrik Sverdrup <ulrik.sverdrup@gmail.com> wrote:

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