[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: '__iter', yet again!
- From: "John Hind" <john.hind@...>
- Date: Wed, 16 Dec 2009 15:03:24 -0000
Yes __call is much less justifiable than __iter, mostly I just use it as a
"bodge" for implementing __iter! However what put this bee back in my bonnet
was running up against a hidden "gotcha" with this bodge. I have written a
library function which returns a 'Table' object which is just a standard
Table with an '__call' based iterator. Trouble is, if you accidently write:
for k, v in library.gettable() do ... end
for k, v in library.gettable()() do ... end
You get a nice little recursion which grows until all your memory is gone!
I do have one (very rare) legitimate use for __call though - if you are
implementing the concept of 'events' it is useful and natural to be able to
write a collection of functions such that "calling" the collection object
calls all the functions in the collection passing them the same parameters.
[mailto:email@example.com] On Behalf Of Francesco Abbate
Sent: 16 December 2009 14:32
To: Lua list
Subject: Re: '__iter', yet again!
For the other side I think that the __call metamethod is weird because
the calling of a function is such a fundamental construct that
overloading it can be confusing when you read the code. For the other
side is can always choose to not overload __call if you prefer so.