lua-users home
lua-l archive

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


2013/9/5 Andrew Starks <andrew.starks@trms.com>:
>> On Sep 4, 2013, at 5:31 PM, Andrew Starks <andrew.starks@trms.com> wrote:
>>
>> > One would not like to be caught redesigning Lua on this list! :)

Oh? It is being done quite often. People who point out that some
suggested extension is actually quite easy to achieve with
currently available features, tend to be tagged as language lawyers.

> Also, my non-existent idea was kinda dumb in that "__next"
> is really the last missing iterator metamethod, not "__in".

Call it "__in", "__next", "__iter", nothing dumb in a name, it's
the idea that counts.

The difficulty is that "in" expects a callable. Anything with a "__call"
metamethod is a valid target for "in".

Unfortunately "__call" works too hard. Some object-oriented styles
use "__call" as a constructor, but then it is not available as an iterator.

So maybe "in" should not expect a callable, it should expect an iterable,
which is anything with an "__iter" metamethod (or "__in", or "__next",
what you will.).

In that case, the question arises what should be done if someone
uses debug.setmetatable to set an "__iter" metamethod for functions.
Should it still be ignored as in the case of "__call" since Lua knows
what to do, or should it now override the calling of the function?