[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __ipairs (was: Re: Lua 5.4 Tuples FTW!)
- From: "John Hind" <john.hind@...>
- Date: Fri, 16 Mar 2018 15:38:37 +0000
>>Hisham wrote:
>>When writing a library, you cannot assume you can monkey-patch other
>>people's standard libraries at will.
>>Not everyone writes Lua code in a situation where they can play God
>>with the environment.
So provide an iterator factory in your library which does what you think
should be done:
for i, v in mytypeiterator(instanceofmytype) do print(i, v) end
Better still make it a method rather than a metamethod of your object
so:
for i, v in instanceofmytype:ipairs() do print(i, v) end
pairs and ipairs were never supposed to be syntax, they are one of any
number of possible implementations of iteration which were intended to
be extended by users and library authors.
Best of all, were my "Self Iterating Objects" patch
(lua-users.org/wiki/LuaPowerPatches) to be adopted, you could make the
iterator appropriate to the type a metamethod __iter of the type and
then your users can write the much more intuitive:
for i, v in instanceofmytype do print(i, v) end
- John
------ Original Message ------
From: "Hisham" <h@hisham.hm>
To: "John Hind" <john.hind@zen.co.uk>; "Lua mailing list"
<lua-l@lists.lua.org>
Subject: Re: __ipairs (was: Re: Lua 5.4 Tuples FTW!)
On 16 March 2018 at 07:56, John Hind <john.hind@zen.co.uk> wrote:
>>>> for i,v in pairs(t) do print(i,v) end
>>A perfect example of why deprecating __ipairs was the wrong move.
It
>>ought to be possible to make this code work with ipairs(t) as well,
but
>>with no __ipairs metamethod it can't be done.
>>Andrew.
Except of course by the simple expedient of replacing ipairs(t) with a
version
that does what you want it to do directly! Even, if you insist, with a
version that
uses a metamethod __ipairs! The metamethods on both pairs and ipairs
have
always baffled me with their utter triviality!
-- Hisham