lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Lua 5.1.2  Copyright (C) 1994-2007, PUC-Rio
% a = {fruit='banana',state='qld'}
% b = {state='nsw'}
% setmetatable(b,{__index=a})
% = b.fruit
% = b.state
% for k,v in pairs(b) do print(k,v) end
%state   nsw
% a[1] = 10
% b[2] = 20
% = b[1]
% = b[2]
% for i,v in ipairs(b) do print(i,v) end

The manual simply says that "the construction

     for k,v in pairs(t) do _body_ end

will iterate over all key-value pairs of table t."

Whilst might been seem as implying that only k-v pairs actually in table t will be returned (i.e. no metamethods pursued), it doesn't explicitly say so (perhaps it should?).

So my question is: why do pairs and ipairs (and, by implication, next) behave this way?