lua-users home
lua-l archive

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


No! You cannot implement '__iter' by re-writing pairs and ipairs, that is
the competing (and I agree , trivial) '__pairs' & '__ipairs' proposal which
provide metatable customisation of pairs and ipairs similar to the way
'__tostring' customises the "tostring" function.

'__iter' is intended to enable you to write:

for v in obj do ... end

Where 'obj' is a table with a metatable containing a function similar to
'pairs' keyed with '__iter'. You have to modify the parser and probably the
byte code interpreter to implement this.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Geoff Leyland
Sent: 16 December 2009 18:26
To: Lua list
Subject: Re: '__iter', yet again!

On 17/12/2009, at 3:32 AM, Francesco Abbate wrote:

Isn't one difference between the two that without metamethods there's no way
you could add special functionality to 'obj[val]' or 'obj1 + obj2', whereas
it's trivial to write your own versions of pairs and ipairs that respect
your own __iter metamethod?

Cheers,
Geoff