Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 520 Bytes

Generalised Iteration.md

File metadata and controls

27 lines (21 loc) · 520 Bytes

#tag

Since the new metamethod __iter was added to every table, all tables can be used as the iterator within for loops, making pairs and ipairs redundant.

Here's an example:

local array = {1, 2, 3}
local dictionary = {a = 1, b = 2, c = 3}

-- old
for index, value in ipairs(array) do
    print(index, value)
end

for key, value in pairs(dictionary) do
    print(key, value)
end

-- new
for index, value in array do
    print(index, value)
end

for key, value in dictionary do
    print(key, value)
end