lua-users home
lua-l archive

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




On Mar 24, 2018, at 2:45 AM, John Hind <john.hind@zen.co.uk> wrote:

'pairs' and 'ipairs' are bog-standard Lua functions and Lua already has perfectly good generic mechanisms for overloading functions. Metamethods for overloading specific functions are unnecessary bloat. Metamethods are only needed to overload syntax, and IMHO should be reserved and restricted for this purpose.

While meta methods for pairs/ipairs might indeed be overkill, I disagree that this is always the case. Take tostring() for example. Would you advocate removing __tostring()? I would argue this is a bad idea. Sure, I can write some kind of tostring() over-ride, or create one for each type of object, but that doesnt help in large projects where I might not have control of (or even access to) all the code using tostring() to generate output.

The fact is, in larger projects, polymorphic/generic functions are indeed very useful, and metamethods are one way to create these. Arguing that pairs() should not have a meta method because “it’s just a Lua function” doesnt seem to me much of an argument. You are better arguing that ipairs() is DEFINED as iterating over all positive numeric indices in order until a nil is returned, which IS a valid argument against __ipairs meta method (which could break this contract).

—Tim