lua-users home
lua-l archive

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


I understand that pairs and ipairs is necessary because of the dual nature
of the table type and I am not proposing this should change. But basic Lua
is not object oriented, metatables are a facility to let users add object
orientation. I often use tables in the way you describe too - it is great to
be able to make a table which is its own invert for example. But I cannot
see why (in an object oriented system) you'd want to support both types of
iteration on the same object.

In any case if you want to have "__pairs" and "__ipairs" you can easily do
that yourself - it does not have to be part of the language. "__iter" is a
feature that would have to be part of the basic language definition. Wanting
to re-implement the table data type using userdata seems like a very
minority interest to be decisive in language design compared to ordinary
object oriented design, particularly when it is already possible (easy) to
do this with the current design!

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-
> bounces@bazar2.conectiva.com.br] On Behalf Of Sam Roberts
> Sent: 10 June 2009 19:11
> To: Lua list
> Subject: Re: Next Version of Lua? (__iter metamethod)
> 
> On Wed, Jun 10, 2009 at 7:41 AM, John Hind<john.hind@zen.co.uk> wrote:
> > Looking at it fundamentally, any object that implements collection
> semantics
> > benefits from iterator functionality. "pairs" is appropriate for
> > dictionary-like collections (whether implemented using tables or
> otherwise)
> > while "ipairs" is appropriate for "list" or "array" like collections
> > (whether implemented using tables or otherwise). With basic Lua
> whether a
> > table is "dictionary-like" or "list-like" is about how you assign the
> keys,
> 
> Its not an "or". Its a fairly common lua design idiom for tables to be
> BOTH dictionary-like and list-like.
> 
> I've never used another language like this, so
> maybe its unusual, but its fundamental to the flavour of the language,
> and
> used to good effect.
> 
> I've built tree-like structures where ipairs() is used to iterate the
> children, and string keys are used for annotations on the node (and
> pairs is used to iterate all keys). There's an example like this in
> _Programming in Lua_, but I can't find it, sorry.
> 
> Anyhow, the __pairs and __ipairs allows userdata to be first-class
> citizens, closer to full table-like functionality, not just limited to
> "array EXCLUSIVE-OR dictionary", and a single iteration strategy.
> 
> > but with true object orientation (using metatables) you will usually
> decide
> > the structure of the table in the class design. OK sometimes an
> object
> 
> Userdata are not required to be used as part of an O-O class design,
> though they can be. Lua is multi-paradigm.
> 
> Cheers,
> Sam