lua-users home
lua-l archive

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


On 16 March 2018 at 12:38, John Hind <john.hind@zen.co.uk> wrote:
>>>Hisham wrote:
>
>>>When writing a library, you cannot assume you can monkey-patch other
>>>people's standard libraries at will.
>
>>>Not everyone writes Lua code in a situation where they can play God
>>>with the environment.
>
> So provide an iterator factory in your library which does what you think
> should be done:

By that logic, why have a __len metamethod if we can just provide
mytypelength(), or any of the other metamethods. :)

Having said that, I personally don't think a metamethod is necessary
for ipairs(), now that Lua (since 5.3) respects the other metamethods.
The protocol for ipairs() is restricted enough so that it should be
possible to produce, using the table metamethods, a behavior that is
customizable and yet respects the protocol expected by users of
ipairs() (ordered integer keys, value is never nil). Removing __ipairs
ensures that this protocol is never broken, so I think it's a good
call.

-- Hisham