lua-users home
lua-l archive

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


> | Message: 7
> | Date: Fri, 24 Jun 2016 04:29:46 -0400
> | From: Sean Conner <sean@conman.org>
> |
> |   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 2013).
> | 
> |   You have been warned.
> |

[John Hind] 
I cannot resist a bump for my '__iter' metamethod, buried in your massive 
haystack of metamethod proposals! Your point is taken, but __iter does 
not add bloat because it replaces any need for other metamethods in this 
area (__ipairs, __pairs, __next etc.). You can find an implementation in 
the PowerPatch WiKi section:

http://lua-users.org/wiki/LuaPowerPatches

I have been applying this to every Lua  version since 2009 and have used it 
extensively in my own practice. The latest versions even include patches for

the manual.

It allows one to write:

for namelist in table_or_userdata do block end

Like in almost every other language on the planet!

I am totally baffled why this does not get adopted as I've never seen any 
serious downside to it. Lua got side-tracked (IMHO) into this notion of 
multiple different iteration strategies for the same object type, but no one

has ever come up with a compelling general use case beyond the original 
pairs and ipairs. On top of this, ipairs is slowly slipping into obscurity
as 
most people tend to prefer the numeric for loop when working with 
list-type and array-type collections. So in almost all real-world cases, 
there is "one right" iterator for any given type of object. This is what
_iter 
provides, without precluding the current method in the few cases 
that justify provision of multiple iterator options. It also provides a 
fallback to a 'pairs' equivalent (for tables only, not userdata) in the 
absence of the metamethod.

__iter is a true metamethod which provides a fallback in a case which 
would otherwise generate an error in the LVM (in contrast to__pairs
 and __ipairs which are rather trivial delegations in library functions - if

they were removed from the standard library they could be easily added 
back, in pure Lua code, by anyone who really wanted them or for
 transitional compatibility).