[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Why don't pairs() and ipairs() obey an __index metamethod?
- From: Duck <duck@...>
- Date: Thu, 20 Sep 2007 13:38:14 +1000 (EST)
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
%
% a = {fruit='banana',state='qld'}
% b = {state='nsw'}
% setmetatable(b,{__index=a})
% = b.fruit
%banana
% = b.state
%nsw
% for k,v in pairs(b) do print(k,v) end
%state nsw
% a[1] = 10
% b[2] = 20
% = b[1]
%10
% = b[2]
%20
% 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?