lua-users home
lua-l archive

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

It was thus said that the Great Duane Leslie once stated:
> Hi,
> Would it be possible to add a `__next` metamethod to allow the `pairs`
> behaviour to be inferred in the same way that now the `ipairs`
> behaviour is inferred from the `__index` metamethod.  I have looked
> through some discussions of this in the mailing list in the past and
> it seems to have been that the `__pairs` metamethod was preferred
> because it was more powerful (it allows the construction of a `next`
> equivalent function as a closure with additional information).
> The problem I have is that I have situations where the `next()`
> function makes more sense to iterate over some userdata objects than
> using `for ... pairs` but I cannot make the behaviour of these two
> methods equivalent.
> The idea would be to have the situation where I could define just the
> `__next` metamethod, and this would be used as the result of the call
> to the `next` function, and so in turn would imply that `pairs` would
> call my `__next` metamethod through the standard `next` function.

  My own archives of the Lua mailing list go back to October of 2009, and
the earliest record I found of __next was on the 15th of January, 2010:

  I'm not saying your idea is bad Duane, but I am using it as a springboard
for the Giant List of Proposed Lua Metamethods since October 2009!  The
following list is probably incomplete and may have some inaccuracies in it,
and I have no information other than the name of the method (I did not
record who first proposed it or even what the final verdict was).  Also, I
only recorded the first instance of a method; not every time it showed up is
listed.  I'm just thinking it might be fun to see how "obvious" some of
these are (my personal favorite: __athashdollarpercentbangbangsplat from

  You have been warned.

  -spc (And yes, work has been a bit slow lately ... )

===[ Proposed ]====