lua-users home
lua-l archive

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


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