[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Adding direct enumeration of collections to Lua generic for loops
- From: Daurnimator <quae@...>
- Date: Wed, 4 Apr 2018 09:57:03 +1000
On 4 April 2018 at 04:15, Jean-Luc Jumpertz <jean-luc@celedev.eu> wrote:
> Direct for..in enumeration can be customized by defining a "__forgen" (for generator) metamethod that shall return the 3 values needed by the generic for loop: an iterator function, a state, and the initial value of the iterator variable.
>
> If no "__forgen" metamethod is defined and the collection is a Lua table, direct for..in enumeration is equivalent to calling the standard Lua library function pair().
Must be that time of the year for the __iter proposal :P
See https://duckduckgo.com/?q=inurl%3Alua-users.org%2Flists%2Flua-l%2F+"__iter"
Generally the outcome is:
- Lua used to do bare iteration but removed it
- There are often multiple ways to iterate an object (see ipairs vs
pairs). It's limiting to pick just one.
- You can already do it with __call