How does the function returned from pairs work?
In for in loop we see this pattern:
local T= {}
T.apple = 10;
T.banana = 5;
T.orange = 4;
for K,V in pairs(T) do
end
How does this actually work under the hood?
The pairs function returns a function which is called an iterator function.
We can call this function to iterate through the keys in the table:
function main(Data)
local T = {}
T.apple = 10;
T.banana = 5;
T.orange = 4;
local Iterator = pairs(T)
trace(Iterator)
local K, V
K,V = Iterator(T, nil)
trace(K,V)
K,V = Iterator(T, K)
trace(K,V)
K,V = Iterator(T, K)
trace(K,V)
K,V = Iterator(T, K)
trace(K,V)
repeat
K,V = Iterator(T, K)
trace(K,V)
until K == nil
end
Â