Add __ipairs metatable event on LuaCustomTable

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Add __ipairs metatable event on LuaCustomTable

Post by aubergine18 »

In Lua 5.2, the __pairs and __ipairs metamethods were added, allowing override of the default pairs/ipairs iterators.

If ipairs(someTable) is called, the ipairs() function first checks someTable's metatable to see if it has an __ipairs event, and, if found, it uses that as the iterator instead.

The custom __ipairs event (function) could check to see if `#` is defined for the custom table (ie. is it an array?), and, if not, revert to the custom __pairs event instead.

IMO this would make working with custom tables much easier, as modders won't have to worry about using pairs/ipairs on such tables, it will "just work", regardless of whether the table is numerically-indexed or not.

Some additional infos can be found here: http://lua-users.org/wiki/GeneralizedPairsAndIpairs

For reference, here's ipairs() implemented in plain lua:

Code: Select all

function ipairs(t)
  local function ipairs_it(t, i)
    i = i+1
    local v = t[i]
    if v ~= nil then
      return i,v
    else
      return nil
    end
  end
  return ipairs_it, t, 0
end
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15141
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add __ipairs metatable event on LuaCustomTable

Post by Rseding91 »

Why? Just don't ever use ipairs.

As I said on reddit when someone else was having issues like this: ipairs is just a shitty version of pairs that works less often.
If you want to get ahold of me I'm almost always on Discord.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Add __ipairs metatable event on LuaCustomTable

Post by aubergine18 »

I was always under the impression that ipairs was faster than pairs, "because numerical referencing"?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15141
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add __ipairs metatable event on LuaCustomTable

Post by Rseding91 »

The code would be identical on the Factorio side if the name was ipairs or pairs.

As for the speed, I've never measured it but if you're iterating over a table that only has indexed items it's going to be the same speed as pairs anyway since the internals work the same way.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding interface requests”