[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua 5.3 `ipairs` considered perilous
- From: Hisham <h@...>
- Date: Tue, 16 Feb 2016 16:20:20 -0200
On 16 February 2016 at 13:19, Dirk Laurie <firstname.lastname@example.org> wrote:
> It seemed such a good idea at the time, `ipairs` respecting the
> __index metamethod. After burning my fingers a few times with
> it, though, I find it simpler not to use `ipairs` at all, instead of
> pondering each time whether ipairs will behave.
> Example 1. XML tables. These are lists in which the items
> are either scalars or XML tables, and which also have some
> "attributes", i.e. string-valued keys associated with scalar values.
> The context is very often one in which the items take default
> attributes from the containing value by specifying it as __index.
> Unfortunately numerical keys are also inherited. If you traverse
> a subtable via 'ipairs', it will look for an item in the parent table
> when the subtable is exhausted. And all the way back to the
> top-level table.
That of course depends on the design of your XML-handling library. I
see it as a problem of the library and not of ipairs. (Out of
curiosity, which library does this? TBH it would surprise me when
reading a subtag if it inherited its parents attributes at all.)