Why does pairs in roblox not return next?

In vanilla Lua, pairs internally is really just

function pairs(t)
    return next, t, nil
end

So it works the same as next, only more idiomatic. But in roblox, this is not the case.

print(pairs({ }) == next) -- prints true in vanilla lua, false in roblox

When i tried this:

print(pairs({ }) == pairs({ }) -- prints false in roblox, true in vanilla lua

I got false. So it looks like roblox made pairs return a new closure instead. Why is this?

1 Like

I’m not that sure, but I think it’s because of the version of Lua Roblox uses. Roblox uses Lua 5.1, I think? Maybe it’s different from the version you are using.

I’m not entirely sure if that is the problem, but do try it out and let me know :smile:.

This is part of a recent optimization by Luau as far as I’m aware. Don’t know the specifics but loops with common iterators run hella faster.

1 Like