Re: __ipairs
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: __ipairs
- From: "Soni \"They/Them\" L." <fakedme@...>
- Date: 2018年3月16日 22:07:19 -0300
On 2018年03月16日 11:17 AM, Hisham wrote:
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!
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.
-- Hisham
Useful snippet to have in your code:
do local mt = {__ipairs=function(t) return t.i end} local t =
setmetatable({i=function() end},mt) if ipairs(t) ~= t.i then local
oldipairs = ipairs; ipairs = --[[monkeypatch here]] end end
Always probe the "standard" libraries, and always fallback to the
previous one. Never assume they haven't been monkeypatched. (Also,
probing helps with performance.)
--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.