[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: '__iter', yet again!
- From: "John Hind" <john.hind@...>
- Date: Wed, 16 Dec 2009 21:29:15 -0000
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.
[mailto:email@example.com] 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?